Home > OS >  with poDoNotEncode as a parameter option of my TCustomRESTRequest a '/' still get encoded
with poDoNotEncode as a parameter option of my TCustomRESTRequest a '/' still get encoded

Time:09-28

I'm trying to make a POST request using TCustomRESTRequest, to add more specific headers needed by the API I'm trying to reach.

As I'm adding headers to my request, I tried to add the specified Content-Type: multipart/form-data:

var
  RESTRequest : TCustomRESTRequest;
  RESTClient : TRESTClient;
  Response : TCustomRESTResponse;

begin
  RESTRequest := TCustomRESTRequest.create(nil);
  RESTClient := TRESTClient.create('');
  try
    RESTClient.BaseURL := 'urltoreach';
    RESTRequest.Client := RESTClient;
    RESTRequest.AddParameter('Content-Type','multipart/form-data',pkHTTPHEADER,poDoNotEncode);
    RESTRequest.Method := rmPOST;
    RESTRequest.Execute;
    Response := RESTRequest.Response;
    if Response.Status.Success then
      begin
        showmessage('success'); 
      end;
    else
      begin
        showmessage(Response.StatusText   ' : '   Response.Content);
      end;
  finally
    RESTRequest.free;
    RESTClient.free;
  end;
end;

The problem I have is that I'm getting an error message:

'Unsupported media type : {"errors":"unsupported media type multipart/form-data"}'

Meaning that my / is getting encoded even though I'm requesting poDoNotEncode as a parameter option.

Do you have any idea why?

CodePudding user response:

Well, for starters, you should be using TRESTRequest instead of TCustomRESTRequest.

But also, do not use TRESTRequest.AddParameter(pkHTTPHEADER) to set the HTTP Content-Type header. TRESTClient/TRESTRequest will determine on their own which Content-Type to use based on the type of body content you are sending.

However, in your example, you are not actually posting any body content at all. See How to post data with a ContentType of 'multipart/form-data' in Delphi REST? and RESTRequest How to send a multipartformdata in a HTTP Post body?

You need to add some body parameters. TRESTClient/TRESTRequest will use multipart/form-data if one of the following conditions is met:

  • there is a parameter using ContentType = ctMULTIPART_FORM_DATA (not sure how you can set this, though)

  • or, there are more than 1 parameter using Kind = pkREQUESTBODY

  • or, there is 1 parameter using Kind = pkREQUESTBODY, and 1 or more parameters using Kind = pkGETorPOST

  • Related