I am trying to populate a already built php database using axios requests, however i keep receiving 422 error and I don't understand what I am missing. Could you please help me :)
This is the error that i get:
xhr.js:177 POST URL/data 422 (Unprocessable Entity)
This is the Post request schema of the DB:
"post": {
"summary": "Post new data row",
"description": "Post new data row",
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"data": {
"type": "string",
"example": "{\"test\":1}"
},
"type": {
"type": "string",
"example": "1"
},
"status": {
"type": "integer",
"example": 1
}
},
"required": [
"data",
"type"
]
}
}
}
},
"responses" :{
"422": {
"description": "Error",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"type": "object",
"properties": {
"error": {
"type": "string",
"example": "Invalid input"
}
This is my code,
I have several switch cases and they should all work in the same manner:
case "rezervariContact" : {
const {type, titlu, description, phone, email} = this.state;
const contactData = {
type: this.state.type,
data:{
type,
data :{
titlu, description, phone, email
}
},
status:true
}
data = {...contactData};
}
}
await axios({
method: "post",
headers: { "Content-Type": "application/json",
'Accept': '*/*' },
body:data,
url : 'http://xxxxxxxxx.ro/data'
})
.then(function (response) {
console.log("RespResolved::",response.config.params);
})
.catch(function (response) {
console.log("catchErrResp::",response);
});
}
Please if you can spot something let me know.
422 probably means your inputted data is invalid.
The reason for this depends on the server. You could try referring to any documentation for the API/server you are trying to reach.
Check document on http://xxxxxxxxxx.ro/data! It means your post data is valid but server can not hand it correctly!
Related
I believe I am successfully posting a new post to the endpoint
https://api.linkedin.com/v2/ugcPosts
using (similar to)
{
"author": "urn:li:organization:1234567",
"lifecycleState": "PUBLISHED",
"specificContent": {
"com.linkedin.ugc.ShareContent": {
"shareCommentary": {
"attributes": [],
"text": "Some share text"
}
}
},
"visibility": {
"com.linkedin.ugc.MemberNetworkVisibility": "PUBLIC"
}
}
The response I get is
{"id":"urn:li:share:01234567890123456789"}
But then when I go to the company page, the post is not visible. Note that the IDs are redacted here. Is there another step I have to take?
edit: I went to verify the post with the endpoint https://api.linkedin.com/v2/ugcPosts/[redacted URN]?viewContext=AUTHOR and my post is a success
{
"lifecycleState": "PUBLISHED",
"specificContent": {
"com.linkedin.ugc.ShareContent": {
"shareCommentary": {
"inferredLocale": "en_US",
"attributes": [],
"text": "Test article sharing article on LinkedIn API , please ignore"
},
"media": [
{
"media": "urn:li:digitalmediaAsset:XXXXXXXXXXXXXX",
"title": {
"attributes": [],
"text": "image"
},
"thumbnails": [],
"status": "READY"
}
],
"shareFeatures": {
"hashtags": []
},
"shareMediaCategory": "IMAGE"
}
},
"visibility": {
"com.linkedin.ugc.MemberNetworkVisibility": "PUBLIC"
},
"created": {
"actor": "urn:li:person:XXXXXXXXXXXXXX",
"time": 1649285262053
},
"author": "urn:li:organization:XXXXXXXXXXXXXX",
"clientApplication": "urn:li:developerApplication:XXXXXXXXXXXXXX",
"versionTag": "0",
"id": "urn:li:share:XXXXXXXXXXXXXX",
"firstPublishedAt": 1649285262053,
"lastModified": {
"actor": "urn:li:csUser:7",
"time": 1649285262122
},
"distribution": {
"externalDistributionChannels": [],
"distributedViaFollowFeed": true,
"feedDistribution": "MAIN_FEED"
},
"contentCertificationRecord": "{\"spamRestriction\":{\"classifications\":[],\"contentQualityClassifications\":[],\"systemName\":\"MACHINE_SYNC\",\"lowQuality\":false,\"contentClassificationTrackingId\":\"073BB82AD64A7608E3F142B6D0362E3D\",\"contentRelevanceClassifications\":[],\"spam\":false},\"originCountryCode\":\"ca\",\"modifiedAt\":1649285262024,\"contentHash\":{\"extractedContentMd5Hash\":\"0725A7E31B109EB4CFB84D2648CE3EC8\",\"lastModifiedAt\":1649285262023}}"
}
What am I missing here?? Why is it now showing on LinkedIn??
edit2 : went ahead and tried to share the post again, now all the posts are gone from the company page when viewed as admin, but showing (except the API posts) as a member.... How broken is this API ??????
I will answer my own post. Uploads using CurlFile DO NOT WORK, you have to use Guzzle... Once the upload & publishing are done, the feed will unbreak itself.. The company page will be broken if the endpoint is still waiting for upload while you post.
$client = new \GuzzleHttp\Client();
$res = $client->request('PUT', $__url, [
'headers' => [
'Authorization' => 'Bearer ' . $__token
],
'body' => fopen($__file, 'r')
]
);
if($res){
$res = json_decode($res->getBody());
}
I'm trying to parse Json result below fromAjax response. Foreach CostingItem i need to access to UnitPrice.Amount, but I got error.
Can someone help me to access items?
I got this response from Laravel based webservice.
{
"CostingItem": [{
"#attributes": {
"Description": "Basic Price",
"Quantity": "1",
"PassengerRPH": "1",
"PriceBasis": "Passenger"
},
"UnitPrice": {
"#attributes": {
"Amount": "1823.82",
"OriginalAmount": "1974.32"
}
},
"ExtendedPrice": {
"#attributes": {
"Amount": "1823.82",
"OriginalAmount": "1974.32"
}
},
"Commission": {
"#attributes": {
"Rate": "11.00",
"Type": "P"
}
}
}, {
"#attributes": {
"Description": "Basic Price",
"Quantity": "1",
"PassengerRPH": "2",
"PriceBasis": "Passenger"
},
"UnitPrice": {
"#attributes": {
"Amount": "1823.82",
"OriginalAmount": "1974.32"
}
},
"ExtendedPrice": {
"#attributes": {
"Amount": "1823.82",
"OriginalAmount": "1974.32"
}
},
"Commission": {
"#attributes": {
"Rate": "11.00",
"Type": "P"
}
}
}]
}
here is the script used in Ajax:
$.ajax({
type: "POST",
url: "/public/fetch-bambini",
data: {
id: d,
tipologia: tipologia,
bambini: bambini,
neonati: neonati,
adulti: adulti
},
success: function(response) {
$.each(response.CostingItem, function(k, v) {
console.log(v);
});
}
});
This return this response:
function ajax()
{
$('form').submit(function() {
console.log($(this).serializeArray());
$('#result').text(JSON.stringify($(this).serializeArray()));
return false;
});
}
After this form I'm getting the json data:
[
{
"name": "firstName",
"value": "fsdfdf"
},
{
"name": "lastName",
"value": "df"
},
{
"name": "emailAddress",
"value": "refdfdfd.56#gmail.com"
},
{
"name": "password",
"value": "fdfdddd"
},
{
"name": "phoneNumber",
"value": "fdfdf"
}
]
I'm sending it to the server by this response.php
<?php
header('Access-Control-Allow-Origin: *');
$json = "http://ec2-54-201-121-123.us-west-2.compute.amazonaws.com:8080/refer247/registration";
$jsonfile = file_get_contents($json);
var_dump($jsonfile);
echo json_encode($_POST);
var_dump(json_decode($jsonfile));
echo json_decode($jsonfile);
?>
But after this I am getting json parser error. What actually is happening? I don't know. When I include datatype as text then my success function is calling but I want to send to the server only json data. What I'm doing wrong here...?
I appreciate if i get some help. Thanks.
I would like to have a custom serialization in Guzzle.
I'm setting a POST application/json request, but my object is serialized with its name (professionalSession) at the beginning, i.e.:
{
professionalSession :
{
param1 : "asdf",
param2 : "jkl;",
...
}
}
That is inconsistent with the REST API I'm trying to call. (className is hidden as one of the parameters).
This is my definition in serviceDescription.json:
"PostAuthentication": {
"httpMethod": "POST",
"uri": "/xxx-person-service/session",
"summary": "Posts the session object",
"type": "json",
"responseClass": "XXX\\WebServicesClientBundle\\Entity\\ProfessionalSession",
"parameters":{
"session": {
"location": "json",
"required": true
},
"session-identifier": {
"location": "header",
"required": true,
"sentAs": "HTTP_X_SESSION_KEY"
}
}
}
I would like to use serviceDescription.json and only override its 1 parameter (by produce json myself).
I tried changing the location of param to body (as it was said in SO somewhere), but Content-Type is not being properly set to application/json.
How can I do it? Thanks!
I will reply as there are no responses yet and I've overcame this problem.
Changing the location of param to body was good approach as it removes that top level JSON element. (This was signaled as a problem, but still - this is how Guzzle behaves.)
To change request to application/json you can use following description in serviceDescription.json:
"PostAuthentication": {
"httpMethod": "POST",
"uri": "/xxx-person-service/session",
"summary": "Posts the session object",
"responseClass": "XXX\\WebServicesClientBundle\\Entity\\ProfessionalSession",
"parameters":{
"session": {
"location": "body",
"required": true
},
"session-identifier": {
"location": "header",
"required": true,
"sentAs": "HTTP_X_SESSION_KEY"
},
//THIS is what you need:
"content-type": {
"location": "header",
"static": true,
"required" : true,
"default" : "application/json",
"sentAs" : "Content-Type"
}
}
},
All,
I have an AJAX request, which makes a JSON request to a server, to get the sync status. The JSON Request and responses are as under: I want to display a JQuery UI progressbar and update the progressbar status, as per the percentage returned in the getStatus JSON response. If the status is "insync", then the progressbar should not appear and a message should be displayed instead. Ex: "Server is in Sync". How can I do this?
//JSON Request to getStatus
{
"header": {
"type": "request"
},
"payload": [
{
"data": null,
"header": {
"action": "load",
}
}
]
}
//JSON Response of getStatus (When status not 100%)
{
"header": {
"type": "response",
"result": 400
},
"payload": [
{
"header": {
"result": 400
},
"data": {
"status": "pending",
"percent": 20
}
}
]
}
//JSON Response of getStatus (When percent is 100%)
{
"header": {
"type": "response",
"result": 400
},
"payload": [
{
"header": {
"result": 400
},
"data": {
"status": "insync"
}
}
]
}
Assuming you want your progress bar/message to be placed in a div named "loadingDiv":
$(document).ready(function() {
var myLoadingDiv = $("#loadingDiv");
myLoadingDiv.progressbar({disabled:true});
$.getJSON("getStatus.php", function(data) {
if (data.payload.data.status == "insync") {
myLoadingDiv.progressbar("disable");
myLoadingDiv.html("Server is in Sync");
}
else if (data.payload.data.status == "pending") {
myLoadingDiv.progressbar("enable");
myLoadingDiv.progressbar("value", data.payload.data.percent);
}
else {
//something else if there are any other status'
}
});
});