Matthew Stott
Matthew Stott

Reputation: 397

Call to API via postman works fine but not in my app

I have made a successful call to a rest API in postman.

I have copied the c# code postman generates and put it in my own c# web API but it fails.

Reporting bad request.

Am I missing something, will postman be doing something extra I'm not aware of.

POSTMAN Headers...

Content-Type : application/json

Body...

{  
   "key":"value",
   "key":"value",
   "key":value,
   "key":"value",
   "key":"{key:value}"
}

The above runs fine...

My Web API...

       string json = " { ";

        json += " \"key\":\"value\", ";
        json += " \"key\":\"value\", ";
        json += " \"key\":\"value\", ";
        json += " \"key\":\"value\", ";
        json += " \"key\":\"{ key: value}\"";

        json += " } ";


        var client = new RestClient("url");
        var request = new RestRequest(Method.POST);
        request.AddHeader("content-type", "application/json; charset=utf-8");
        request.AddParameter("application/json", json, ParameterType.RequestBody);
        IRestResponse response = client.Execute(request);

This returns...

StatusCode: BadRequest, Content-Type: text/html, Content-Length: -1)

Upvotes: 0

Views: 10400

Answers (1)

Matthew Stott
Matthew Stott

Reputation: 397

After all that, all it needed was the url to be https:// not http:// :(

Just in case it helps anyone else in the future, in postman the url was just set as domain.co.uk but in my code I had put http://domain.co.uk for the base url of the API, so the API required all calls to be made using SSL.

Postman must do SSL by default. As soon as I set my app to https:// it worked fine.

Upvotes: 5

Related Questions