POST requests to WP API are interpreted as GET requests - php

I'm trying to create an order from Woocommerce API and it is not working as intended: the request (sent as POST) is returning all orders (like it would be a GET request), instead of creating a new one.
The reald odd thing is that the same exact request is working on the pre-production server but not on the production server.
This seems to be a global issue with the API, as other requests (like creating a post from the WP API) are not working, except the POST request used to get the access token.
Here is the request I send as POST:
curl -X POST https://www.domain.tld/wp-json/wc/v2/orders?access_token=... \
-H "Content-Type: application/json" \
-d '{
"customer_id": "1",
"payment_method": "app",
"payment_method_title": "Test payment",
"set_paid": false,
"billing": {
"first_name": "test",
"last_name": "test",
"address_1": "test",
"address_2": "test",
"city": "test",
"postcode": "00000",
"country": "FR",
"phone": "0123456789",
"email": "test#test.tld"
},
"shipping": {
"first_name": "test",
"last_name": "test",
"address_1": "test",
"address_2": "test",
"city": "test",
"postcode": "00000",
"country": "FR",
"phone": "0123456789",
"email": "test#test.tld"
},
"shipping_lines": [
{
"method_id": "livraison_gratuite",
"method_title": "Livraison gratuite",
"total": 0
}
],
"line_items": [
{
"product_id": 302,
"variation_id": 589,
"quantity": 1
},
{
"product_id": 798,
"quantity": 1
}
]
}'
Again the same request is working on pre-production server so I don't think the problem is related to the request itself.
Here is the return I get in postman for this request on production server:
I eliminated all the potentiel causes:
Both websites are using the same plugins, Wordpress version and plugins are both up to date,
Both websites are using https,
Cache plugin has been deactivated,
API settings in Woocommerce and WP Oauth Server are identical,
User used for sending the request (identified with the access token provided by WP Oauth Server) is admin,
Server configuration is the same so far as I know (PHP7).
I'm running out of idea about why this is happening. Anyone have a clue about what could cause this?

I finally found out what was happening here. There is a redirection rule on the production server that add the trailing slash to URL when it is missing. This was causing the request recognized as GET instead of POST (POST data are not sent when HTTP request is redirected).
Adding a trailing slash fixed the issue:
curl -X POST https://www.domain.tld/wp-json/wc/v2/orders/?access_token=...
As a note, this can be used as well on the pre-production server that doesn't use any redirect rule of this kind, so always adding a trailing slash to POST API requests can be a good idea for not stumbling upon this kind of issue.
An alternative solution can be to add a RewriteCond of this kind to prevent the redirection for POST requests:
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteCond %{THE_REQUEST} !POST
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]

Related

PHP: I want to know how to send a POST request with everything in this json for my PHP SERVER

This is for my personal sharex imagehost.
{
"Version": "14.1.0",
"Name": "Discord post",
"DestinationType": "ImageUploader",
"RequestMethod": "POST",
"RequestURL": "https://discord.com/api/v9/channels/CHANNEL HERE/messages",
"Headers": {
"authorization": "Discord Token here"
},
"Body": "MultipartFormData",
"Arguments": {
"content": null,
"tts": "false"
},
"FileFormName": "file",
"URL": "{json:['attachments'][0]['url']}",
"ThumbnailURL": "{json:['attachments'][0]['url']}"
}
This json is a sharex custom uploader*
I also want to get a response too.
Please write down the code for me bc im new to php with post requests.
I tried looking at multiple answered questions and I couldn't find one that shows how to use headers and other stuff.
Nevermind, I found out discord rate-limited my website for some reason.

Integration of payments with PayU

I have a problem with integration of payments on PayU .
I am not an advanced programmer , but I want to do this on my website.
I created an sandbox account on the https://www.payu.pl/en .
I was browsing forum but I still don't understand .
First I used this code : https://repl.it/#PayU/pop-up-widget .
But it's probably a combination of the store itself and the website.
Now probably i must use this code and create order from http://developers.payu.com/en/restapi.html#creating_new_order_api
curl -X POST https://secure.snd.payu.com/api/v2_1/orders \
-H "Content-Type: application/json" \
-H "Authorization: Bearer d9a4536e-62ba-4f60-8017-6053211d3f47" \
-d '{
"notifyUrl": "https://your.eshop.com/notify",
"customerIp": "127.0.0.1",
"merchantPosId": "300746",
"description": "RTV market",
"currencyCode": "PLN",
"totalAmount": "21000",
"buyer": {
"email": "john.doe#example.com",
"phone": "654111654",
"firstName": "John",
"lastName": "Doe",
"language": "pl"
},
"settings":{
"invoiceDisabled":"true"
},
"products": [
{
"name": "Wireless Mouse for Laptop",
"unitPrice": "15000",
"quantity": "1"
},
{
"name": "HDMI cable",
"unitPrice": "6000",
"quantity": "1"
}
]
}'
If it's the correct code, how can I use it?
To which format to save this file?
How to run this script.
I have never used curl and I don't know how to go about it.
The code You posted is a command, which will send an HTTP POST-REQUEST (see https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol).
The request basically consists of:
The destination address (https://secure.snd.payu.com/api/v2_1/orders), which identifies the server and the path for the request on the server, such that the server can execute the request appropriately.
Two headers, the first informs, which content type the message has and the second provides some kind of identification / authorization.
The message body itself, which provides the server with the actual content of the request.
This request is executed by means of a terminal command (curl), so to execute it, You have several options. Either, You paste this code exactly like it is show into a terminal to execute it directly, or You save it into a normal text file, which ends on .sh and execute that (after making it executable). These methods of course only work provided You have a unix like operating system and the curl command line utility is in your executable path. However, there are multiply ways of sending an http request, curl is not necessarily needed. There are even some online forms, which allow doing that. Even PayU provides one, which You can find, if You click on the "Try it now" button on the page You just found the command on.
So this answers your questions. However I still have a few remarks:
Is it the correct code? It works, I tested it. But since You created an own sandbox account, You are probably supposed to change the line Authorization: Bearer d9a4536e-62ba-4f60-8017-6053211d3f47, such that it contains the authorization code, which You probably received.
If You want to execute this code on Your website, You need to find out, how to do the request using the language You use for web-developing (e.g. PHP). Then You probably don't need to use curl, but the function in that language using the parts of the request, I pointed out above, appropriately. But that would probably be a new question here.
Make a comment, if You do not understand parts of my answer and I will extend it.

Why am I not getting a valid response from this REST API web service with Postman?

I have a API web URL which only accepts POST commands for searching it’s contents. I have authorized it with an API –KEY and an unique ID in the header tab of postman and put all the requires fields in the Body tab .
I am getting a response from the server saying that they got my request because I am getting a request_id back. But for some reason I am getting the response status as “ERROR”. I am not sure what’s going on . What could be the reason that’s causing the server to accept my request, but not able to reply back ? I been trying to get any response from this server that makes sense for the past 6 hours.
Here are some of the data fields I am sending the server in the Body.
{
"device_id_type": "aaid",
"device_id": "BAC9BA51-4C51-45C8-8091-3F6890A21A70",
"app_version": "1.1",
"country": "US",
"q": "hello",
"offset": 0,
"limit": 20,
"locale": "en_US",
"tz_offset": -5,
"gender": "m",
"age": "21-24",
"geo": "40.7506, 73.9935"
}
Response from the server:
{
"request_id": "ec78cd56c7da4c078d8ee5d456c52c71",
"status": "ERROR",
"data": {
"error": {
"code": 500,
"message": "An unexpected error has occured"
}
}
}
Response from server screenshot
Code Snippet from postman

Return a Json file via get request

(ionic 3 app)
let me walk you through, whats going on here.
i have a file information.json, which contains information about various categories.
{
"items": [
{
"name":"Phones and Tablets",
"category_id": "20",
"image": "http://somesite/image/cache/catalog/menu-icons/phone-tablet-icon-500x500.png",
"children": [
{
"name": "Smartphones",
"category_id": "60"
},
{
"name": "Tablets",
"category_id": "62"
},
{
"name": "Accessories",
"category_id": "63"
},
{
"name": "Basic Gsm Phones",
"category_id": "64"
}
]
},
which is just sitting there looking pretty in main directory on my site.
when i maka a get request.
var headers = new Headers();
headers.append('accept', 'application/json');
let options = new RequestOptions({ headers: headers, });
this.http.get("http://somesite/information.json", options)
.map(data => data.json().items)
.subscribe(data => {
this.information = (data);
console.log(this.information);
}
)}
i get CORS error in console.
Failed to load http://somesite/information.json: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access.
i have a Moesif origin & Cross changer chrome extension, when i enable that then everything works fine in browser.
when i build/run the app on android everything seems to work fine.
but when i run the same app on ios, it doesn't seem to get any response.
i dont know if ios block all CORS by default or something.
i tried fiddling around with .htaccess file of my site, i included
Headers Allow-Access-Control-Origin "*", which seems to work for only 1 request, so i was able to get categories information. but all other get requests from my app became invalid. currently i am looking around to set up a PHP file with headers information so when i make a get request to php, it echos out that file information.json , but the problem is i just dont know how to set it up properly.
if you guys have any other solution for this problem, feels free to share them.
thanks.

Json Ouput for URL shows "image":"data\/demo\/hp_banner.jpg" in opencart

This is the output for echo json_encode. // uses getBanner() built-in function of opencart.
[
{
"banner_image_id": "77",
"banner_id": "6",
"link": "index.php?route=product/manufacturer/info&manufacturer_id=7",
"image": "data/demo/hp_banner.jpg",
"language_id": "1",
"title": "HP Banner"
}
]
The output in app is:
data\ /demo\ /hp_banner.jpg
The urls are what I am concerned about, as I have to use them on a Java App.
They can be fixed from java but I wonder why the URLs are that way?
Thank you
Use JSONObject class to decode the JSON at the client end. It will automatically remove those braces and slashes.

Categories