I have an API. In this API, I pass X-AUTH_TOKEN & X-AUTH-KEY as headers and in the body, I pass raw array data ["1", "2", "3"....]
I have tested it using postman, it is returning me response.
But when I implement it using symfony, I am getting HTTP/2 404 returned for "
This is what I have implemented in Symfony.
$response = $this->httpClient->request('POST', $url, [
'headers' => [
'x-auth-token' => $authToken,
'x-auth-key' => $authKey
],
'body' => [
"1",
"2",
"3",
"4",
]
]);
Can anybody please help me how can I implement?
Thank You.
Instead of body pass json
$response = $this->httpClient->request('POST', $url, [
'headers' => [
'x-auth-token' => $authToken,
'x-auth-key' => $authKey
],
'body' => [
"1",
"2",
"3",
"4",
]
]);
Related
I am sending request using curl.
this is json data :
{
"users": [
{
"userId": "123",
"userLink" : "www.example .com",
"userType": "au"
}
]
}
I am trying this but i need same above json:
$post = array(
"users" => array(
"userId" => "123",
"userLink" => "link",
"userType" => "au"
)
);
$out = array_values($post);
$post = json_encode($out);
print_r($post);
You need an additional array dimension, and if you use brackets [ ] you can easily see how they look the same:
$post = [
"users" => [
[
"userId" => "123",
"userLink" => "link",
"userType" => "au"
]
]
];
$post = json_encode($post);
Also, don't use array_values; that will remove the string key users.
I'm trying to send an email via Dynamic template in SendGrid using Guzzle HTTP in PHP.
But I was not able to send mail. As I get only the error like below without any reason in it.
Type: GuzzleHttp\Exception\ClientException
Message: Client error: `POST https://api.sendgrid.com/v3/mail/send` resulted in a `400 Bad Request` response: {"errors":[{"message":"Bad Request","field":null,"help":null}]}
My PHP example code:
require __DIR__.'../../vendor/autoload.php';
$CLIENT = new GuzzleHttp\Client();
$response = $CLIENT->request('POST', 'https://api.sendgrid.com/v3/mail/send', [
"headers" => [
"Authorization" => "Bearer my-api-key",
"Content-Type" => "application/json"
],
'data' => '{
"from": {
"email": "admin#example.com"
},
"personalizations": [{
"to": [{
"email": "me#gmail.com"
}],
"dynamic_template_data": {
"email_data": [{
"id": "2",
"title": "Artificial Intelligence in Health Care",
"image": "https://example.com//uploads/3663581583995181_0724.jpg",
"description": "Immediate application of AI in the Health Care domains."
}, {
"id": "199",
"title": "Aesthetics Skill Discussion 3 by Jranand",
"image": "",
"description": "Aesthetics Skill Discussion 3 by Jranand"
}]
}
}],
"template_id": "my-template-id"
}',
]);
echo $response->getStatusCode();
I was able to send an email via the dynamic template test method in SendGrid with the same dynamic_template_data. But trying this with Guzzle HTTP. I m not able to find the reason for the error.
Able to send an email with dynamic JSON data in the testing.
Can anyone help me out to solve the problem?
Thanks in advance.
There is no data request option in json, you need to change your code either you can use json request option from guzzle(will need lesser effort) or you can directly right your body.
try{
$CLIENT = new GuzzleHttp\Client();
$data = [
"from" => [
"email" => "admin#example.com"
],
"personalizations" => [
[
"to" => [
[
"email" => "me#gmail.com"
]
],
"dynamic_template_data" => [
"email_data" => [
[
"id" => "2",
"title" => "Artificial Intelligence in Health Care",
"image" => "https://example.com//uploads/3663581583995181_0724.jpg",
"description"=> "Immediate application of AI in the Health Care domains."
],
[
"id" => "199",
"title" => "Aesthetics Skill Discussion 3 by Jranand",
"image" => "",
"description" => "Aesthetics Skill Discussion 3 by Jranand"
]
]
]
]
],
"template_id" => "my-template-id"
];
$response = $CLIENT->request('POST', 'https://api.sendgrid.com/v3/mail/send', [
"headers" => [
"Authorization" => "Bearer my-api-key",
"Content-Type" => "application/json"
],
'json' => $data,
]);
if ($guzzleResponse->getStatusCode() == 200) {
$response = json_decode($guzzleResponse->getBody(),true);
//perform your action with $response
}
}
catch(\GuzzleHttp\Exception\RequestException $e){
// you can catch here 400 response errors and 500 response errors and log those errors
}catch(Exception $e){
//other errors
}
I'm working with Elasticsearch-PHP client. I want to index my datas, but I have a problem with mapping. There is a problem: I create my data array, everything works fine, but when I add this array to my index body => my_data_array some datas show up, but not all of them. I don't know why. I just digged and try all of following steps but nothing changed.
I just attached my snippets.
This my controller file where I index datas:
{
$params = ['body' => []];
foreach($all_ads as $key => $ads){
$params['body'][] = [
'index' => [
'_index' => 'demo_data',
'_type' => 'demo',
'_id' => $ads->id
]
];
$params['body'][] = $ads->indexParams();
}
$responses = $client->bulk($params);
this is result json object:
"response": [
{
"id": 85345,
"old_id": "5088063",
"user_id": "2706",
"category_id": "15",
"type": "3",
"title": array[3],
"slug": "",
"sub_region_id": "8",
"condition": "1",
"username": "John Doe",
"price": "82000",
"price_type": "1",
"no_phone": "0",
"views": "29",
"hot": "0",
"vip": "0",
"price_measure": "0"
}
I have a datafield and it is not visible here.
Its data mapping structure
'data' => [
'type' => 'object',
'properties' => [
'key_id' => ['type' => 'integer'],
'value_id' => ['type' => 'integer'],
'key' => ['type' => 'keyword'],
'value' => ['type' => 'keyword']
]
]
How can I fix it?
I just check some question from here and github issue forum but nothing helps. Thanks!
You have to follow solution of the question. Its maybe helpful for you.
Elasticsearch mapping not working as expected
Ask Question
I need to make a POST request to claim voucher. At API docs I find this:
POST /vouchers/{voucherId}/claim
{
"Participant": {
"FirstName": "John",
"LastName": "Jones",
"Telephone": "99999 127 127",
"EmailAddress": "hahahah#hahaha.net",
"PostCode": "HP18 9HX",
"HouseNameNumber": "2",
"Street": "Bridge Road",
"Locality": "Ickford",
"Town": "Lodnon",
"County": "Bucks"
},
"ExperienceDate": "2015-10-01T00:00:00"
}
Now I, using Laravel guzzle library I make this request:
public function testclaim()
{
$client = new GuzzleHttp\Client;
$headers = ['Content-Type' => 'application/json'];
$res = $client->post('https://apidev.asdasd.com/vouchers/11989898_1-9FDD/claim', [
'headers'=>$headers,
'auth' => [
'PET_RES', 'asdasdasd111111'
],
'form_params' => [
'FirstName' => 'Peter',
'LastName' => 'Alexo',
'Telephone' => '8888888888888'
]
]);
$res = json_decode($res->getBody()->getContents(), true);
dd($res);
}
but what I get is:
400 Bad Request
{ "Message": "The request is invalid.", "ModelState": { "claim": [ "An
error has occurred." ] (truncated...)
What is the right way to send a request to the API with following data?
try this
...
'form_params' => [
'Participant' => [
'FirstName' => 'Peter',
'LastName' => 'Alexo',
'Telephone' => '8888888888888'
],
'ExperienceDate' => '2015-10-01T00:00:00'
]
...
if your api just accept json, try replace form_params with json
I'm using browserstack screenshots API - https://www.browserstack.com/screenshots/api The following curl is working:
curl -u "username:password" -H "Content-Type: application/json" -H "Accept: application/json" -d '{"browsers": [{"os": "Windows", "os_version": "7", "browser_version": "8.0", "browser": "ie"}], "url": "http://google.com"}' http://www.browserstack.com/screenshots
However, when I try the same call call with guzzle I get 422 Unprocessable Entity error.
$client = new GuzzleHttp\Client();
$request = $client->post('http://www.browserstack.com/screenshots', [
'headers' => ['Content-type' => 'application/json'],
'auth' => ['username', 'password']
]
);
$data = ['browsers' => ['os' => 'Windows', 'os_version' => '7', 'browser_version' => '8.0', 'browser' => 'ie'], 'url' => 'http://google.com'];
$request->setBody($data);
$response = $request->send();
dd($response);
Can you suggest how to debug this issue?
I had a mistake in method. It should be $client->createRequest instead of $client->post
Also, I had a mistake in data being passed. Browsers should be array of arrays
There is another library that could be used with browserstack and guzzle: https://github.com/ksenzee/browserstack-screenshots-php
$client = new GuzzleHttp\Client();
$request = $client->createRequest('POST', 'http://www.browserstack.com/screenshots', [
'headers' => ['Content-type' => 'application/json'],
'auth' => ['user', 'pwd'],
'body' => '{"browsers": [
{"os": "Windows", "os_version": "7", "browser_version": "8.0", "browser": "ie"},
{"os": "android", "os_version": "4.4", "device": "HTC One M8", "browser": "Android Browser"}
],
"url": "http://www.lipsum.com"}'
]
);
$response = $client->send($request);
dd($response->json());
To send JSON to the Screenshots API, you would need to format it as a JSON string.
$client = new GuzzleHttp\Client();
$request = $client->post('http://www.browserstack.com/screenshots', [
'headers' => ['Content-type' => 'application/json'],
'auth' => ['username', 'access_key'],
'body' => '{"browsers": [
{"os": "Windows", "os_version": "7", "browser_version": "8.0", "browser": "ie"},
{"os": "android", "os_version": "4.4", "device": "HTC One M8", "browser": "Android Browser"}
],
"url": "http://www.lipsum.com"}'
]
);
You will then be able to view the progress on your BrowserStack Screenshots page.
For a guide on how to send POST requests using Guzzle, you can refer this documentation — https://media.readthedocs.org/pdf/guzzle/latest/guzzle.pdf.
In my case uses 'json' in options's argument (https://es.stackoverflow.com/questions/185183/porqu%C3%A9-guzzle-5-0-lanza-el-error-422-si-estoy-armando-bien-la-consulta#185192), example:
$client = new GuzzleHttp\Client(['base_url' => 'http://www.browserstack.com/']);
$request = $client->post('screenshots', [
'headers' => ['Content-type' => 'application/json'],
'auth' => ['username', 'access_key'],
'json' => '{"browsers": [
{"os": "Windows", "os_version": "7", "browser_version": "8.0", "browser": "ie"},
{"os": "android", "os_version": "4.4", "device": "HTC One M8", "browser": "Android Browser"}
],
"url": "http://www.lipsum.com"}'
]
);