API json postman and drupal 8 - php

How are you my friends ?
i have an issue with API with drupal 8 in post method
when i send the request the respond is 406 Not Acceptable
this is the code
POST /movies/entity/node?_format=hal_json HTTP/1.1
Host: abdallah.tech
X-CSRF-Token: t1UOL3VNN0GRGSNxWN************************
Authorization: Basic ************************
Cache-Control: no-cache
Postman-Token: 4f61c400-4916-4dfb-8ca7-0ed08c48a4bc
{
"_links": {
"type": {
"href": "http://abdallah.tech:8083/movies/rest/node/add/movies"
}
},
"type": [{
"target_id": "movies"
}],
"title": [{
"value": "hello world"
}],
"body": [{
"value": "some body content aaa bbb ccc"
}]
}'
can you help me with that please

just try to change
POST /movies/entity/node?_format=hal_json HTTP/1.1
to
POST /movies/node?_format=hal_json
// or
POST /node?_format=hal_json

Related

PHP able to receive post request with a raw text but its giving error when post same data raw json

I'm posting data to my API endpoint. it's simple JSON data. But I'm getting error 404 when posting data as raw JSON but if I post the same data at the same endpoint as raw text its works.
working as raw text
getting error 404 as raw JSON
<?php
var_dump(http_response_code());
echo "hello";
var_dump($_POST);
echo file_get_contents("php://input");
I have removed all code from the API end point and just trying to print post data.
Sample JSON :
{
"object": "whatsapp_business_account",
"entry": [
{
"id": "456",
"changes": [
{
"value": {
"messaging_product": "whatsapp",
"metadata": {
"display_phone_number": "123456789",
"phone_number_id": 123456789
},
"contacts": [
{
"profile": {
"name": "NAME"
},
"wa_id": 123456789
}
],
"messages": [
{
"from": 123456789,
"id": "wamid.ID",
"timestamp": 123456789,
"text": {
"body": "MESSAGE_BODY"
},
"type": "text"
}
]
},
"field": "messages"
}
]
}
]
}
I have checked JSON and it's a valid JSON. no special character but still unable to solve this issue.
One more thing its working on my local machine under XAMPP but not on Linux shared server.

Send Current URL link to A Discord Webhook

So, basically I need a way to send the current URL link to A Discord webhook.
So for example, the URL would be https://www.example.com and I need that link to send to my Discord webhook.
Is there any way someone could help me with this? If this is not possible, are there any alternative methods?
I would recommend you first reading this—it explains in detail how to format the payload so that it renders as a message in Discord properly, you can even use markdown!
Second, I'm not sure what language you want to code this in, but here's a simple example using Python and httpx
The idea to take away here is
Pick a client library
Study the Discord webhook message format
Create a client for HTTP
Set the logic to send the payload to the webhook you want
Create and send the payload with the formatted webhook message in a POST request
data = {
"username": "Webhook",
"avatar_url": "https://i.imgur.com/4M34hi2.png",
"content": "Text message. Up to 2000 characters.",
"embeds": [
{
"author": {
"name": "Birdie♫",
"url": "https://www.reddit.com/r/cats/",
"icon_url": "https://i.imgur.com/R66g1Pe.jpg"
},
"title": "Title",
"url": "https://google.com/",
"description": "Text message. You can use Markdown here. *Italic* **bold** __underline__ ~~strikeout~~ [hyperlink](https://google.com) `code`",
"color": 15258703,
"fields": [
{
"name": "Text",
"value": "More text",
"inline": true
},
{
"name": "Even more text",
"value": "Yup",
"inline": true
},
{
"name": "Use `\"inline\": true` parameter, if you want to display fields in the same line.",
"value": "okay..."
},
{
"name": "Thanks!",
"value": "You're welcome :wink:"
}
],
"thumbnail": {
"url": "https://upload.wikimedia.org/wikipedia/commons/3/38/4-Nature-Wallpapers-2014-1_ukaavUI.jpg"
},
"image": {
"url": "https://upload.wikimedia.org/wikipedia/commons/5/5a/A_picture_from_China_every_day_108.jpg"
},
"footer": {
"text": "Woah! So cool! :smirk:",
"icon_url": "https://i.imgur.com/fKL31aD.jpg"
}
}
]
}
import httpx # pip install httpx, or poetry add httpx
with httpx.Client() as client:
request = client.post("https://www.discordwebhook.com", data=data) # needs to be a POST
print(request.status_code)

API gives 406 code with Accept application/json request : Bluehost

This is the result I get when I try to validate my API.
Content-Type: application/json
Vary: Accept
{
"valid": false,
"token_uri": "https://mining4btc.com/NFT/rinkeby/CKE/1",
"errors": [
"InvalidTokenUrlResponseException: Invalid response, expected 200 but got 406 for URL: https://mining4btc.com/NFT/rinkeby/CKE/1"
]
}
I'm using this to validate my API:
https://testnets-api.opensea.io/asset/0xe8dD349E3B0F0FA0eE063a2D99541155aFEf14B9/1/validate/
As you can see, it's getting a 406 code.
This is the API:
<?php
header('Content-Type: application/json');
$output = '{
"attributes": [
{
"trait_type": "Eyes",
"value": "cute"
},
{
"trait_type": "Nose",
"value": "cute"
},
{
"trait_type": "Cheeks",
"value": "cute"
},
{
"trait_type": "Toes",
"value": "cute"
},
{
"trait_type": "Ability",
"value": "chairs"
},
{
"display_type": "boost_number",
"trait_type": "Cuteness",
"value": 5000
},
{
"display_type": "number",
"trait_type": "Age",
"value": 1
}
],
"description": "Thinking about my Mommy.",
"external_url": "https://mining4btc.com/NFT/rinkeby/CKE/external",
"image": "https://mining4btc.com/NFT/rinkeby/CKE/proof/1.jpg",
"name": "Chillin"
}';
$decoded = json_decode($output);
echo json_encode($decoded);
?>
This is a contract API for a NFT following the OpenSea documents; deployed with Truffle to rinkeby.
I have confirmed that this is because Bluehost actively blocks (by returning this code) requests with Accept:application/json headers. You have to contact Bluehost and ask them to whitelist the IP addresses that you want to be able to access the metadata.
As far as I can tell, they do this because they want to push people to pay for a VPS, because they are assuming this kind of request would go to some kind of robust API that requires significant backend processing.

Getting randomly 302 on Ajax Request using Cakephp

I have a system that is developed in cakephp. There I have a customer profile page that shows me different information like e.g notes, phone calls, attached files. The basic information are fetched via Cakephp other information like the above are fetched via Ajax Requests and shown as soon as the data is fetched.
Unfortunately, sometimes and very randomly (between every 10th and 100th request), I get a 302 of an Ajax Request and will logged out and redirected to the login formular (which is the basic redirection path if you are not logged in).
You can see an example from the Google network page in the pictures. The picture shows that the calls getPhonecalls, getNotes and getOutboxBook are fine, but the getFilesBackoffice call returns a 302 and afterwards triggers the login page. Also the getAllowedFiles afterwards is called fine. It is important to say, that it is always a different Ajax Call and therefore I assume, any Ajax call can cause the problem. A successful Ajax call returns a JSON.
I tried to reconstruct the problem, but it for me it seems to be mission impossible to reconstruct it. I also tried to debug that problem, and tried to check what the error might be... but no chance. Also the php Error Log gives no information.
So next, I downloaded the full "Network Information" from Chrome with "save as HAR with content". Looking at that file, it seems, like the Cookie (with its Session) is deleted.
Here is an excerpt of the HAR with the status 302:
"response": {
"status": 302,
"statusText": "Found",
"httpVersion": "HTTP/1.1",
"headers": [
{
"name": "Date",
"value": "Thu, 20 Dec 2018 09:47:27 GMT"
},
{
"name": "Server",
"value": "Apache/2.4.12 (Unix) OpenSSL/1.0.1p PHP/5.6.11 mod_perl/2.0.8-dev Perl/v5.16.3"
},
{
"name": "X-Powered-By",
"value": "PHP/5.6.11"
},
{
"name": "Content-Type",
"value": "text/html; charset=UTF-8"
},
{
"name": "Location",
"value": "http://localhost/stressfrei/HRMAppMig/Users/login"
},
{
"name": "Connection",
"value": "Keep-Alive"
},
{
"name": "Set-Cookie",
"value": "CAKEPHP=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; Max-Age=0; path=/"
},
{
"name": "Set-Cookie",
"value": "CAKEPHP=2e5gke00430jdkd8ll5jl3ini3; expires=Fri, 21-Dec-2018 09:47:28 GMT; Max-Age=86400; path=/stressfrei/HRMAppMig/; HttpOnly"
},
{
"name": "Set-Cookie",
"value": "CAKEPHP=o3udvthe0rkmjt5djrjvnt3392; expires=Fri, 21-Dec-2018 09:47:28 GMT; Max-Age=86400; path=/stressfrei/HRMAppMig/; HttpOnly"
},
{
"name": "Keep-Alive",
"value": "timeout=5, max=95"
},
{
"name": "Content-Length",
"value": "0"
}
],
"cookies": [
{
"name": "CAKEPHP",
"value": "deleted",
"path": "/",
"expires": "2018-12-20T09:47:27.127Z",
"httpOnly": false,
"secure": false
},
{
"name": "CAKEPHP",
"value": "2e5gke00430jdkd8ll5jl3ini3",
"path": "/stressfrei/HRMAppMig/",
"expires": "2018-12-21T09:47:27.127Z",
"httpOnly": true,
"secure": false
},
{
"name": "CAKEPHP",
"value": "o3udvthe0rkmjt5djrjvnt3392",
"path": "/stressfrei/HRMAppMig/",
"expires": "2018-12-21T09:47:27.127Z",
"httpOnly": true,
"secure": false
}
],
"content": {
"size": 0,
"mimeType": "text/html",
"compression": 0
},
"redirectURL": "http://localhost/stressfrei/HRMAppMig/Users/login",
"headersSize": 717,
"bodySize": 0,
"_transferSize": 717
}
Here is an excerpt of the 200
"response": {
"status": 200,
"statusText": "OK",
"httpVersion": "HTTP/1.1",
"headers": [
{
"name": "Date",
"value": "Thu, 20 Dec 2018 09:52:39 GMT"
},
{
"name": "Server",
"value": "Apache/2.4.12 (Unix) OpenSSL/1.0.1p PHP/5.6.11 mod_perl/2.0.8-dev Perl/v5.16.3"
},
{
"name": "Connection",
"value": "Keep-Alive"
},
{
"name": "X-Powered-By",
"value": "PHP/5.6.11"
},
{
"name": "Content-Length",
"value": "2239"
},
{
"name": "Keep-Alive",
"value": "timeout=5, max=97"
},
{
"name": "Content-Type",
"value": "text/html; charset=UTF-8"
}
],
"cookies": [],
"content": {
"size": 2239,
"mimeType": "text/html",
"compression": 0,
"text": "HERE IS SOME JSON"
},
"redirectURL": "",
"headersSize": 287,
"bodySize": 2239,
"_transferSize": 2526
},
Here is an example of my ajax call.
this.ajaxCall = function(offset) {
var class_prefixTemp = this.class_prefix;
var self = this;
$.ajax({
//ajax options
type: "POST",
url: self.ajaxUrl,
data: {"offset": offset, "elements_per_page": this.elements_per_page, "conditions": this.conditions, "recursive": this.recursive},
dataType: 'json',
//handling return
success: function(result) {
//print out the data from the result
}
});
}
Do you have any idea, how I can find out what my problem is to finally solve it?
Finally the problem was the setting of the Session in the core.php.
In the core.php of CakePhp it is possible to set for the session autoRegenrate to true. This caused the problem that a new session is generated when a new page is loaded. This can cause that long Ajax Requests are working with a wrong session id because it was renewed and therefore the session id didn't fit anymore. This caused a logout.
Finally I had to disable the renewing of the session by setting autoRegenerate to false!

send json request using php for and api

I am working on some api in which I send information using json.
POST /api/v3/users/ HTTP/1.1
Host: example.com
Content-Type: application/vnd.api+json
Authorization: Bearer ab1dcb45be7e43db2212b0d2b0ca3892
This is the information. Using this will get that response but I don't know how to use this in php and not use js?
get response:
{
"users": [ {
"username": "username",
"phone": "06123",
"last_name": "myname",
"address": "some addr",
"newsletter": true,
"dob": "1974-12-31",
"x_id": "123ext",
"email": "user2#mail.nl",
"email_verified": true,
"social" : {
"facebook": { "id": "100000730165158"},
"twitter": {"id": "139029213"},
"linkedin": {"id": "7EA06I71K5"},
"google": {"id": "104763295979778440139"}
}
}]
}
But I don't know how to use this, meaning how to send request.
You can store this content in avariable, and then just json_decode it to convert it into php array.

Categories