SiBrit
SiBrit

Reputation: 1542

How to force TRESTRequest.ContentType to be ctAPPLICATION_JSON

Drop a TRESTClient, TRESTRequest and TRESTRepsonse onto a form.

Set the RESTClient.BaseURL, RESTRequest.Method and Resource, also add a "Content-Type" header parameter with a value of "application/json".

Add a JSON string using RESTRequest.AddBody, then view the RESTRequest.ContentType.

It shows ctAPPLICATION_X_WWW_FORM_URLENCODED instead of ctAPPLICATION_JSON. This causes the server to return an error when RESTRequest.Execute() is run.

How do I force the request to use the correct content type when the property cannot be assigned to?

Upvotes: 4

Views: 3071

Answers (2)

Jan Bird
Jan Bird

Reputation: 11

This works for me:

client.AddParameter('Content-Type', 'multipart/form-data', pkHTTPHEADER, [poDoNotEncode]);

Upvotes: 1

SiBrit
SiBrit

Reputation: 1542

After looking at the REST.Client source code, if you specify the content type using this:

  AParameter := RESTRequest.Params.AddItem;
  AParameter.ContentType := ctAPPLICATION_JSON;
  AParameter.name := 'Content-Type';
  AParameter.Value := 'application/json';

Instead of this:

  RESTRequest.Params.AddHeader('Content-Type', 'application/json');

Then the TRESTRequest.ContentType property returns with the correct value, and this is the value used during TRESTRequest.Execute.

Another way to force the TRESTRequest.ContentType to be correct when using a body is to add the body text this way:

  RESTRequest.Body.Add(AJSONString, ctAPPLICATION_JSON);

Upvotes: 7

Related Questions