I am trying to update the invoice template of an Xero invoice. My issue is that I get a 400 error and a response saying:
NoDataProcessedException No data has been processed for this endpoint. This endpoint is expecting Invoice data to be specifed in the request body
So my authentication works perfectly but its claiming that I have no data in the body of my post request. As you can see in my code below I have specified data in the body of the request. So have I just formatted it wrong or am I doing something else wrong?
$xeroInvoiceId = '7b601c90-642b-4b6c-9a21-3969ff339cb0';
$updateInvoiceArgs = array(
'headers' => array(
'xero-tenant-id' => $tenantId,
'Authorization' => 'Bearer ' . $refreshTokenBody->access_token,
),
'body' => array(
'InvoiceID' => $xeroInvoiceId,
'BrandingThemeID' => '5d4dd402-c851-497e-aae1-9ff265c0d15a'
)
);
$res = wp_remote_post('https://api.xero.com/api.xro/2.0/Invoices/' . $xeroInvoiceId, $updateInvoiceArgs);
var_dump($res);
You are trying to post template or invoice data?
Related
I am trying to connect to the Listmonk API through wp_remote_post() on WordPress. I created a custom plugin for this.
Some parts work, but when I need to submit data that is an array or integer, I get errors. Also when I try to use wp_json_encode(), as recommended online, the API gives me a 400 error.
I hope someone can help!
I tried a lot of things. My POST code is:
$response = wp_remote_post($listmonk_url, array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode($listmonk_username . ':' . $listmonk_password),
'Content-Type: application/json',
),
'body' => $body,
));
And my body content is:
$body = array(
'name' => $name,
'email' => $email,
'status'=> 'enabled',
'lists' => 2,
'preconfirm_subscriptions' => true,
) ;
Whenever I try to use wp_json_encode() on $body, the API gives me:
[http_response] => WP_HTTP_Requests_Response Object ( [response:protected] => Requests_Response Object ( [body] => {"message":"Invalid email."} [raw] => HTTP/1.1 400 Bad Request Server
The encoded JSON was:
{"name":"john","email":"jan#jan.com","status":"enabled"}
And when I don't use json encoding, my 'lists' never passes through correctly, as you can see below, where the returned JSON shows lists is empty:
"lists":[]}
Is there something I am not seeing?
I've tried dozens of times to debug this, but I can't find where my error code. The problem is simple, I just want to post data with the application/json format
$this->client = new GuzzleHttp\Client(['base_uri' => 'https://myendpointapi.com/']);
$headers = [
'Authorization' => $auth,
'Accept'=>'application/json',
'Content-Type' => $contentTypes
];
$url = "my-service";
$data = ["foo" => "bar"];
$this->client->send('POST',$url,["headers" => $headers, "json" => $data ]);
but the error I cant post $data, the response inform me no data send by me. I am also try to change $data value like
$data = array(["foo" => "bar"])
or change the format to multipart request (application/x-www-form-urlencoded) but the response keep same. I don't know why my code is not send the data.
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.
I am trying to update collection.liquid using Shopify API.
I am using below Shopify API wrapper with CodeIgniter,
Shopify API wrapper
This wrapper uses cURL to make API calls. I have used this library to make other apps for Shopify and it works just fine with GET,POST methods. For the first time I have tried using PUT method with it. and its giving me cURL Error given below ERROR #22: The requested URL returned error: 404 Not Found"
protected function modify_asset($theme_id)
{
$data = array(
'API_KEY' => $this->config->item('shopify_api_key'),
'API_SECRET' => $this->config->item('shopify_secret'),
'SHOP_DOMAIN' => $this->session->userdata('shop_domain'),
'ACCESS_TOKEN' => $this->session->userdata('access_token')
);
$this->load->library('shopify', $data);
$fields = array(
"asset" => array(
"key" => "templates\/collection.liquid",
"value" => "<p>We are busy updating the store for you and will be back within 10 hours.<\/p>"
)
);
$args = array(
'URL' => '/admin/themes/163760333/assets.json',
'METHOD' => 'PUT',
'RETURNARRAY' => TRUE,
'DATA' => $fields
);
try{
$modification_response = $this->shopify->call($args);
return $modification_response;
}
catch(Exception $e){
$modification_response = $e->getMessage();
log_message('error','In Get Active Theme Id' . $modification_response);
//redirect('/wrong/index');
var_dump('In modification response ' . $modification_response);
exit;
}
}
}
Above is my function to implement the API call. You can see cURL options and its implementation on below link:
cURL options and happening of Shopify API call
Note : This request is working just fine on POSTMAN.
I've just been running some tests on this code using the information you provided, and was able to get a successful submission after removing the backslash in $fields['asset']['key'] as per the example above.
So
"key" => "templates\/collection.liquid",
Becomes:
"key" => "templates/collection.liquid",
It appears Shopify doesn't require forward slashes in file keys to be escaped.
I have a problem. I should do a custom gateway. I know the basics. I read the documentation. URL data to be transmitted (such as user name, the amount of the transaction). My question is how to redirect the user to the payment page of the bank? What is the command and where to give the exact url? And then the returned data must be processed what method? cURL or something else? I could not find any real solution to the problem.
Different gateway's have different needs, if your gateway uses a POST, you can use this to POST the data, and get a response.. it is better than cURL.
$response = wp_remote_post( $environment_url, array(
'method' => 'POST',
'body' => http_build_query( $payload ),
'timeout' => 90,
'sslverify' => false,
) );
// Retrieve the body's response if no errors found
$response_body = wp_remote_retrieve_body( $response );
$response_headers = wp_remote_retrieve_headers( $response );
// Payload would look something like this.
$payload = array(
"amount" => $order.get_total(),
"reference" => $order->get_order_number(),
"orderid" => $order->id,
"return_url" => $this->get_return_url($order) //return to thank you page.
);
//use this if you need to redirect the user to the payment page of the bank.
$querystring = http_build_query( $payload );
return array(
'result' => 'success',
'redirect' => $environment_url . '?' . $querystring,
);