I'm trying to submit an application to Greenhouse with the following way:
$url = "https://api.greenhouse.io/v1/boards/{MY_BOARD_TOKEN}/jobs/{MY_JOB_ID}";
$args = [
'headers' => [
'Content-Type' => 'multipart/form-data',
'Authorization' => 'Basic ' . base64_encode('{MY_API_TOKEN}'),
'Cache-Control' => 'no-cache',
],
'body' => $form,
];
$response = wp_remote_post($url, $args);
But I'm getting the following error:
{"status":400,"error":"Failed to save person"}
My $form looks like this:
[
'first_name' => 'John',
'last_name' => 'Doe',
'email' => 'john#doe.com',
]
I'm sure the credentials are OK.
Thanks in advance,
Status code: 400 Bad Request
The 400 (Bad Request) status code indicates that the server cannot or
will not process the request due to something that is perceived to be
a client error (e.g., malformed request syntax, invalid request
message framing, or deceptive request routing).
Link
Means that need to double check you request to API.
I suggest to test it in some other tool and copy&paste request after that.
You could use Restlet Client - REST API Testing for checking.
Related
I have two app in Laravel. One is simple api with one controller if i send params using online tool or postman app everything is ok. i need check in headers to send 'Host' without this header i got error 400 Bad Request. But if i try send the same from my second laravel app i've got empty params request
$response = Http::asForm()->withHeaders([
'Host' => 'example.com:8091',
])->post('http://example.com:8091/abc/test/', [
'param_1' => 'abcdefgh123',
'param2' => 'blablabla',
]);
$body = $response->getBody()->getContents();
I tried asForm() and nothing. What should be in Host header ?
Try this option
$json_data = json_encode(['param_1' => 'test 1', 'param_2' => 'test 2']);
$response = Http::asForm()->post('http://example.com:8091/abc/test/', [
'body' => $json_data,
'headers' => [
'Content-Type' => 'application/json',
'Content-Length' => strlen($json_data),
]
]);
http://labelary.com/viewer.html
This webservice has an 'undocumented' service where you can post them a png, and then send you back the image encoded as a zpl file (for label printers). don't worry, I'm not trying to do anything they don't want me to do, it's mentioned at the bottom here that they allow this undocumented use of their api
So, when you use the api from within the web portal, it makes a post request to this url: http://api.labelary.com/v1/graphics
If I upload a png and then look in the Network tab of Chrome developer tools, I can see that it posted a Form Data, file: (binary)
In their documentation, they actually recommend this Ruby library: https://github.com/rjocoleman/labelary -- this has implemented into it a way to send the post request with the file up to that url and get the zpl data back.
If you navigate to labelary/lib/labelary/image.rb you can see the code for the encode function:
def encode
response = Labelary::Client.connection.post '/v1/graphics', { file: #file }, { Accept: 'application/json' }
image = response.body
return '^GFA,' + image['totalBytes'].to_s + ',' + image['totalBytes'].to_s + ',' + image['rowBytes'].to_s + ',' + image['data'] + '^FS'
end
It makes that request using the faraday request library, if that's of any relevance.
So basically, I'm trying to implement this request in php+laravel using Guzzle. Now I know how to make a post request, but I don't exactly know how to send an image, and I searched around and came up with this code, which isn't working:
$pngPath = storage_path('image.png'); // this is a confirmed real file on my system in the storage folder
$client = new \GuzzleHttp\Client();
$response = $client->request('POST', 'http://api.labelary.com/v1/graphics', [
'headers' => [
'Content-Type' => 'multipart/form-data'
],
'multipart' => [
[
'name' => 'file',
'contents' => fopen($pngPath, 'r'),
]
]
]);
When I make that request, I get this error message that doesn't have much detail: ERROR: HTTP 400 Bad Request
I just needed to modify a bit of my request and it worked -- I think it might have been the Content-Type header messing me up more than anything.
This is what the request looks like now:
$client = new \GuzzleHttp\Client();
$response = $client->request('POST', 'http://api.labelary.com/v1/graphics', [
'headers' => [
// 'Content-Type' => 'multipart/form-data'
'Accept' => 'application/json'
],
'multipart' => [
[
'Content-Type' => 'image/png',
'name' => 'file',
'contents' => fopen($pngPath, 'r'),
'filename' => basename($pngPath)
]
]
]);
Sorry for wasting everyone's time!
I am trying to make a post on laravel using Guzzle. I already got one post working but that one doesn't have a header that I need. The problem is when I try to include that same header and raw JSON body I always get the error: MethodNotAllowedHttpException. From my understanding this is the post request function with some structural error (just my thinking).
My code is the following:
$response = $client->request('POST', $url, [
'headers' => [
'Content-Type' => 'application/json',
'x-auth-token' => $token,
],
'body' => $body
]);
The code above is returning the error.
The following code (from a different function with different goals) just doesn't have the 'x-auth-token' header and it is working just fine:
$response = $client->request('POST', $url, [
'headers' => [
'Content-Type' => 'application/json',
],
'body' => $body
]);
$token = $response->getHeader('X-Subject-Token')[0];
Update: The error that i get is: Expecting to find auth in request body. The server could not comply with the request sin.
I have the following code that I use whenever I want to make POST requests using Guzzle:
$request = $client->request('POST', $url, [
'form_params' => $params,
'headers' => [
'Referer' => '(intentionally removed)',
'Accept' => 'application/json',
]
]);
The code works without any issues and the information within $params is always sent, however when I change the request type from POST to PUT so that the request becomes:
$request = $client->request('PUT', $url, [
'form_params' => $params,
'headers' => [
'Referer' => '(intentionally removed)',
'Accept' => 'application/json',
]
]);
The request suddenly stops sending the data contained within $params.
I have tested the endpoint the request is send to with Insomnia with both POST and PUT requests and both types are processed as expected, so I am certain the issue is not there.
What can be causing the data from Guzzle to be send using the POST method but not when using the PUT?
This behaviour described in guzzle documentation form-params
form_params - Used to send an application/x-www-form-urlencoded POST request.
Probably, you enough pass the parameters in json format:
$request = $client->request('PUT', $url, [
'json' => $params,
'headers' => [
'Referer' => '(intentionally removed)',
'Accept' => 'application/json',
]
]);
I'm trying to delete files from my CloudFlare cache using PHP. Using Guzzle I've done this:
$client = new \GuzzleHttp\Client;
$response = $client->delete('https://api.cloudflare.com/client/v4/zones/myzoneid/purge_cache', [
'query' => [
'files' => 'https://example.com/styles.css,
],
'headers' => [
'X-Auth-Email' => 'myemail',
'X-Auth-Key' => 'myapikey',
],
]);
But when I run this I get an error:
Client error: DELETE https://api.cloudflare.com/client/v4/zones/myzoneid/purge_cache?files=https%3A%2F%2Fexample.com/etc resulted in a 400 Bad Request response: {"success":false,"errors":[{"code":1012,"message":"Request must contain one of \"purge_everything\", \"files\", \"tags\" (truncated...)
I can't get it to work using Postman either. I put in the required headers and try to set a key of files or files[] with the URL but it doesn't work. I've also tried data with raw JSON as the value like {"files":["url"]} (along with a JSON content-type header) but get the same error. It thinks I'm not sending the files key.
The method for purge_cache is POST instead of DELETE (Source: https://api.cloudflare.com/#zone-purge-files-by-url).
The payload is not sent as 'query', but as 'json'.
Files should be an array, not a string.
So the correct syntax should be....
$client = new \GuzzleHttp\Client;
$response = $client->post('https://api.cloudflare.com/client/v4/zones/myzoneid/purge_cache', [
'json' => [
'files' => ['https://example.com/styles.css'],
],
'headers' => [
'X-Auth-Email' => 'myemail',
'X-Auth-Key' => 'myapikey',
],
]);