1
I am trying to communicate with the API of an Ecommerce and all dozens of attempts that did only receive as response "HTTP request failed 400 HTTP/1.1 400 Bad Request".
So I have been informed I have to send a post and on body inform consumer_key, consumer_secret and code.
One important thing is that I have to pass this information on the body of the post and not as parameters.
I tested by Postman using x-www-form-urlencoded, informed the url and put the above information in Body and it worked.
At Delphi I’m sending in the form below:
lHTTP:= TIdHTTP.Create(nil);
IdSSLIOHandlerSocketOpenSSL := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
StringList := TStringList.Create;
StringStream := TStringStream.Create('', TEncoding.UTF8);
StringList.Add('consumer_key = abcdefg');
StringList.Add('&consumer_secret = hijklmn');
StringList.Add('&code = opqrstuvxz');
StringStream.WriteString(StringList.text);
lHTTP.Request.CustomHeaders.Clear;
lHTTP.IOHandler := IdSSLIOHandlerSocketOpenSSL;
lHTTP.Request.ContentType := 'application/x-www-form-urlencoded';
lHTTP.Request.CharSet:= 'utf-8';
lHTTP.HandleRedirects := True;
lHTTP.Request.UserAgent := 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36';
lHTTP.Request.Method:='POST';
Result:= lHTTP.Post(Url, StringStream);
Below is the PHP example provided by Ecommerce:
$params["consumer_key"] = "### Chave da Aplicação ###";
$params["consumer_secret"] = "### Chave Secreta da Aplicação ###";
$params["code"] = "### Código de Autorização ###";
$url = "https://{api_address}/auth/?".http_build_query($params);
ob_start();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_exec($ch);
What’s missing? Or what I’m doing wrong ?
Thank you in advance.
It was the same mistake. I had tried to do so before.
– Marcello Cainelli
Sorry, I had been trying without &. I removed the spaces and the error changed: HTTP request failed 401 HTTP/1.1 401 Unauthorized
– Marcello Cainelli
It worked my friend. Thank you very much. The hard thing is to know that I spent two days banging my head because of the blank spaces. But it worked, let’s move on!
– Marcello Cainelli
@Marcellocainelli please mark the answer as correct
– Guilherme Nascimento