Reputation: 2417
I have tried making a HTTP request using CURL
as below:
$rawQuery = '{
"CUSTNAME" : "1970188",
"CURDATE":"2020-12-28T00:00:00+02:00",
"BOOKNUM":"Test BookNum",
"DETAILS":"Test Details"
}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://somelink.co.de");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "username:password");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
));
curl_setopt($ch, CURLOPT_POSTFIELDS, $rawQuery);
$curl_response = curl_exec($ch);
This one is returning me result. But when I try to implement the same using Symfony HTTP Client, I am getting 400 error.
This is the code, I have tried.
$response = $this->client->request('POST', $url, [
'auth_basic' => [
'username',
'password'
],
'headers' => [
'Accept' => 'application/json',
],
'json' => $rawQuery
]);
I am not sure what I am missing in Client
Can anybody please help me ?
Upvotes: 1
Views: 1056
Reputation: 1
The $rawQuery variable contains a JSON-formatted string representing the data to be sent in the request body.
curl_init()
initializes a new cURL session.
curl_setopt()
is used to set various options for the cURL transfer. Here are the options being set:
CURLOPT_URL:
The URL to which the request will be sent (https://somelink.co.de in this case).
CURLOPT_HTTPAUTH:
The HTTP authentication method to be used. It's set to CURLAUTH_BASIC
, indicating basic authentication.
CURLOPT_USERPWD:
The username and password for basic authentication in the format "username:password".
CURLOPT_RETURNTRANSFER:
Setting this option to 1 tells cURL to return the transfer as a string instead of outputting it directly.
CURLOPT_POST:
Set to 1 to specify that we want to perform an HTTP POST request.
CURLOPT_HTTPHEADER:
An array of HTTP header fields to be included in the request. In this case, it specifies that the request body is in JSON format.
CURLOPT_POSTFIELDS:
The data to be sent as the request body. In this case, it's the JSON data from the $rawQuery variable.
curl_exec($ch)
executes the cURL session and performs the HTTP POST request. The response from the server is stored in the $curl_response variable.
After this code executes, the variable $curl_response will contain the response received from the server as a string. Depending on the API or server response, the contents of $curl_response will vary. It's important to handle the response accordingly, such as checking for errors or processing the data sent back by the server.
Additionally, you may want to include error handling and exception management in your code to handle situations where the cURL request fails or the response is not as expected.
Upvotes: 0
Reputation: 410
Your Raw query is incorrect.. This should be a php array, Symfony is trying to json_encode() this for you when you use the 'json' key in the request. Reformat your $rawQuery to be
$rawQuery = [
"CUSTNAME" => "1970188",
"CURDATE" => "2020-12-28T00:00:00+02:00",
"BOOKNUM" => "Test BookNum",
"DETAILS" => "Test Details"
];
Upvotes: 0