-2

I am using RestSharp to make a POST call to an external API.

I am adding the payload via:

request.AddParameter({field_name}, {field_value})

The API accepts { "name": "value" } (correct json format) but does NOT accept { name: "value" }

If you attempt to make a call with an incorrect json object it will throw an error similar to the following:

"{"type":"https://www.rfc-editor.org/rfc/rfc7231#section-6.5.1","title":"One or more validation errors occurred.","status":400,"traceId":"|87c7263f-4ff49301d1865eed.","errors":{"$":["'n' is an invalid start of a value. Path: $ | LineNumber: 0 | BytePositionInLine: 0."]}}"

This might be due to the following issue

I am getting this same error message when trying to make the POST call through request.AddParameter({field_name}, {field_value})

Is there a way for me to make a request using RestSharp that allows a format similar to { "name": "value" } ?

Community
  • 1
  • 1
Angel
  • 543
  • 5
  • 13
  • 1
    Are you familiar with the tool Postman? If you can get your request working there Postman can generate RestSharp code. – Crowcoder Jan 29 '20 at 00:09
  • Please provide minimal reproducible code. What json are you sending to what api? – Jawad Jan 29 '20 at 01:07
  • @Crowcoder thanks! I was able to reproduce results taking the code that Postman provided ! – Angel Jan 29 '20 at 17:38

1 Answers1

0

Check the documentation

On a POST or PUT Requests, it depends on whether or not you have files attached to a Request. If not, the Parameters will be sent as the body of the request in the form name1=value1&name2=value2. Also, the request will be sent as application/x-www-form-urlencoded.

The correct approach to do this is:

var myJson = new { /*<field_name> = <field_value>, <field2> = <value2> ... etc*/ }
request.AddJsonBody(myJson);
jalsh
  • 801
  • 6
  • 18