Return a Json file via get request - php

(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.

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.

Python Request POST to PHP

I'm trying to submit a form to automate the process of downloading archived data from FIRMS. I've tried inspecting what is being posted using the developer tools as suggested in this response, and the only thing I see being posted is "email" and "cst" (I have no idea what 'cst' is, and it changes each time I refresh the page).
Although I can see the names of the other elements, adding them to the POST request does nothing.
data = {
"region": "custom",
"coord": [llcrnrlon, llcrnrlat, urcrnrlon, urcrnrlat],
"source": "MODIS C6",
"fromDate": startDate,
"toDate": endDate,
"output_format": "csv",
"email": "some_email#gmail.com",
}
# submit the request
response = requests.post(
'https://firms.modaps.eosdis.nasa.gov/download/create.php',
data=data)
Thanks for your help!

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

POST requests to WP API are interpreted as GET requests

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]

guzzle php post request response issue

I am able to make a post request with guzzle php using the following code
$request = $this->request('POST', $this->url, array('form_params' => $params));
Everything works fine. But when I call
$request->getBody()->getContents();
A string "root" is attached to the beginning of the content returned.
I dont seems to understand why this is happening.
Any assistance will be appreciated.
An example of what I get when I var_dump is this
string(4) "root"
{"access_token":"kjVbpzmk3VAWTHn3jyeaM1nal1zkFIPZrI8khmKQ",
"token_type":"Bearer",
"expires_in":604800,
"user_id":3,
"user":{
"id":3,
"name":"Thomas Paul"
}
}
Meanwhile in postman I get this
{
"access_token": "y9Jeovb3EERC4oE13yCS8WfFi3XK1eul4D4luwX3",
"token_type": "Bearer",
"expires_in": 604800,
"user_id": 3,
"user": {
"id": 3,
"name": "Thomas Paul"
}
}
This is a security measure as there is a known security vulnerability in some browsers.
Also the JSON API specs require this top level element.
Guzzle is a really robust library. Maybe Postman isn't or maybe Postman removes the root element by itself... I don't know this.
I solved the problem using a
substr($request->getBody()->getContents(), 17)
of the response and was able to remove the unwanted strings and decoded it to json

Categories