简体   繁体   中英

Getting '405 Method not allowed' when accessing a web API through my ASP.NET web form

I used JSON to pass my variables as per the request of my API provider... I am attaching the code below:

string url = "http://api-v2.happay.in/auth/v1/adduser";
string token = "fcd853a15a6e97b8834255dde74cd99527";

string dataToUpload = "{"requestedId":"12389","userId":"134474","firstName":"Maredu Laxmi","lastName":"","emailId":"arar@gmail.com","mobileNo":"096518644","dob":"1978-02-26","gender":"Female","title":"Ms","password":"","metaFields":{"EmpId":"12389","Grade":"D6","Location":"Hyderabad"},"supervisors":[{"supervisorId":"1024","roleName":"RP"},{"supervisorId":"1025","roleName":"RP"}]}";

var cli = new WebClient();

cli.Headers[HttpRequestHeader.ContentType] = "application/json";
cli.Headers[HttpRequestHeader.Authorization] = "Bearer " + token;

string response = cli.UploadString(url, "POST", dataToUpload);

What am I doing wrong?

405 Method Not Allowed means that the endpoint you're trying to call ( http://api-v2.happay.in/auth/v1/adduser ) doesn't support the method you're trying to use ( POST ).

From RFC 7231, Section 6.5.5 :

The 405 (Method Not Allowed) status code indicates that the method received in the request-line is known by the origin server but not supported by the target resource.

Are you sure the endpoint supports the POST method?

The RFC also states that

The origin server MUST generate an Allow header field in a 405 response containing a list of the target resource's currently supported methods.

You could check the response and see if they've followed the specification and included an Allow header.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM