WebRequest with Content-Type: application/x-www-form-urlencoded returns 403 Forbidden error

To add comments, please log in or register
D3nn1s
39
D3nn1s  
Hi all!


I use the following code :

   int lastErrorTemp = -1;
   string headers,result_headers; 
   char post[],result[]; 
   string url = urlPar; //-- Valid URL

   string payload= "parameterA=testA&parameterB=testB";  //-- Valid Parameters    
   StringToCharArray(payload,post,0,WHOLE_ARRAY,CP_UTF8);
   headers = "Content-Type: application/x-www-form-urlencoded\r\n";      
   //--- Reset the last error code 
   ResetLastError(); 
   int timeout=5000; 
   //--
   int res=WebRequest("POST",url,headers,timeout,post,result,result_headers); 
   if(res==-1){ 
      int errorTemp = GetLastError();
      Print("Error : "+(string)errorTemp); 
      return; 
   }
   ///-- convert from char to string
   string strTemp = "";
   for(int i = 0; i < ArraySize(result); i++){
      strTemp += CharToString(result[i]);
   }
   
   Print("---------------------------------------");
   string resultStr = "url : "+url+" - ";
   resultStr += "\n Result : "+strTemp;
   resultStr += "\n Headers : "+result_headers;
   Print(resultStr);
   Print("---------------------------------------");


   

And for some reason it returns 403 Forbidden error.

If i use other programming language like python or the postman it works properly. So it doesn't a server-side issue.

Last, when i leave the char array post empty then it returns 200 OK.

Any help will be appreciated!

Thanks.




Marco vd Heijden
Moderator
13019
Marco vd Heijden  
urlPar; //-- Valid URL

Is not a valid url.

Remember you can only use post 80 for HTTP and 443 for HTTPS.

And the link has to be added to the list of allowed url's in the settings.

D3nn1s
39
D3nn1s  
Marco vd Heijden:

Is not a valid url.

Remember you can only use post 80 for HTTP and 443 for HTTPS.

And the link has to be added to the list of allowed url's in the settings.

Thanks for the reply.

I wrote in comment 'valid url' because i successfully completed the request with WebRequest function but i had to remove the line 'StringToCharArray(payload,post,0,WHOLE_ARRAY,CP_UTF8);'.

Which means that i left the char array post empty. So when i fill that array i have the 403 Forbidden error. 

Alain Verleyen
40390
Alain Verleyen  
D3nn1s:

Thanks for the reply.

I wrote in comment 'valid url' because i successfully completed the request with WebRequest function but i had to remove the line 'StringToCharArray(payload,post,0,WHOLE_ARRAY,CP_UTF8);'.

Which means that i left the char array post empty. So when i fill that array i have the 403 Forbidden error. 

Try

StringToCharArray(payload,post,0,StringLen(payload),CP_UTF8);
D3nn1s
39
D3nn1s  
Alain Verleyen:

Try

Thank you Alain!

That was the problem. 
Do you know why the change from WHOLE_ARRAY to StringLen(payload) solve that issue ?

Regards!

Alain Verleyen
40390
Alain Verleyen  
D3nn1s:

Thank you Alain!

That was the problem. 
Do you know why the change from WHOLE_ARRAY to StringLen(payload) solve that issue ?

Regards!

Because string in mql are terminated by '\0' and you using WHOLE_ARRAY you get a 0 at the end of your array. The server don't like it.
To add comments, please log in or register