how to send a post to apigility with resclientemozilla? - mysql

how can I send JSON data to apigility using an HTTP POST? I'm trying to send this data:
{
"nombre_pais": "MEXICO",
"id": "20",
"artist": "JUAN AGUIRRES",
"title": "CRAZY",
"genero": "Balada, Pop",
"ocupacion": "Cantante, Productos",
"sitio_web": "www.juanAGUIRRE.com",
}
but I get an error:
{
"type": "http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html",
"title": "Bad Request",
"status": 400,
"detail": "JSON decoding error: Syntax error, malformed JSON"
}
What should I do? Thank you.

I guess when someone tells you "JSON decoding error: Syntax error, malformed JSON" you should check the syntax of your JSON. http://jsonlint.com/ is one of many online validators. You will find that you have an extra trailing comma after the last value.
Every common language now has the ability to automatically create JSON, don't try doing it yourself.

Related

How to handle json API response errors in dart?

I was trying to create a library to parse a few JSON http responses from an API so I made the request and created a model class and parsed the JSON response but sometimes (maybe if the user token expires), an exception with error 401 is returned, and since the error id is required to handle different errors I was thinking of creating another model class for the error and returning that to the user, but that would mean I'll have to either return the class with the required data or the class with the error information which is not possible to do with the same function at a time. Now, if I did a try-catch clause it wouldn't return anything if an exception occurs returning null (which I don't want).
The correct JSON response looks like the following:
{
"result": "ok",
"token": {
"session": "string",
"refresh": "string"
}
}
and the error JSON response will look like the following:
{
"result": "error",
"errors": [
{
"id": "string",
"status": 0,
"title": "string",
"detail": "string"
}
]
}
Now, of course I could do an if else clause checking the response code and doing the rest but again I'll somehow have to return the error class with all the detail in it. How do I do so?
Note: I'm using the mangadex api
I'd make a function that returns Future<Token>
If the result is okay, parse Token and return it.
If it's an error, throw a custom Error implementation with all of the details.
Then you create an idiomatic Dart API for your RPC.

JSON Schema Validation in Mule: get failing field

I am using APIkit in Mule with RAML 0.8 and a JSON schema, as follows (example):
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"cart": {
"title": "",
"description": "",
"type": "object",
"properties": {
"internalNumber": {
"type": "integer"
}
},
"required": [
"internalNumber"
]
}
},
"required": [
"cart"
]
}
and in the Mule Flow, I catch the exception and show the following result:
#[exception.cause.message]
When a validation error occurs, I want to get the name of the field in which the validation failed. Instead, this is what I got:
Input
{
"cart": {
"internalNumber": "I must be an integer"
}
}
Output
"instance type (string) does not match any allowed primitive type (allowed: ["integer"])"
Expected output
{
"field": "cart.internalNumber",
"error": "instance type (string) does not match any allowed primitive type (allowed: ["integer"])"
}
All I want to know is if there is a way to get the name of the field in which the validation errors occurs.
Regarding the Mule Documentation, I can get the whole JSON string but not the name of the failing field...
I hope someone can give me a better solution.
Thanks!
Within your JSON Schema, add "required":"true" attribute, to make the fields mandatory.
You can also use JSON schema validator, in your mule flow, by referring to the updated schema.
Any of the case should through you an error with missing field.
Use below expression to get expected error message.
{
"errorMessage": "#[exception].toString().replace("\"","\\\"")"
}
Not sure if you are expecting it as an output or looking for a way to validate your input and schema.
I can try to suggest on "All I want to know is if there is a way to get the name of the field in wich the validation errors occurs."; to do this better validate your JSON and input data through online validator before defining definitions. Like using http://www.jsonschemavalidator.net/, it will help you with error and fields. Hope this may help!

json regex error

I wrote a json regex for url redirection validation
"redirect_uri": {
"type": "string",
"pattern": "^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,8})([\/\w \.-]*)*\/?$",
"description":"Application redirect_uri"
},
I am getting this error
illegal backslash escape sequence in string, at character offset 1223 (before "\da-z\.-]+)\.([a-...")
Where I am making mistakes . Everything seems to be fine to me
I'm not sure if it's satisfy your pattern requirement but this is a valid JSON:
"redirect_uri": {
"type": "string",
"pattern": "^(https?:\/\/)?([\\da-z\\.-]+)\\.([a-z\\.]{2,8})([\\/\\w \\.-]*)*\\/?$",
"description": "Application redirect_uri"
}
I just add some escape \ where needed.
I recommend to use some online JSON validator site like: http://jsonlint.com/

REST API JSON error code and error message

I have been writing Web Services from a recent past, this is a sample success and error response.
error
{
"code": 1150,
"status": false,
"message": "API Student does not exist.",
"serverTime": "2013-11-29 09:47:52"
}
success
{
"code": 200,
"status": true,
"data": {
"id": 49
},
"serverTime": "2014-04-17 05:06:17"
}
With regards to returning errors I have a confusion, why do we always return one error code and one message, for example, when username and password is required as input params, say a blank request is made, so what I return is a error code 1100 and error message "Username is incorrect". I never return the whole list of errors, for example, in this case, two error message with two error codes should be sent so it saves end users data & time.
This is a sample of what I suggest?
{
"code": 1010,
"status": false,
"errors": [
{
"code": 1000,
"name": "Username invalid"
},
{
"code": 1001,
"name": "Password invalid"
},
{
"code": 1002,
"name": "Password not strong"
}
],
"serverTime": "2013-12-03 12:34:02"
}
Why is this not a good way to do? I have not seen this in either Twitter API or Facebook API.
Nobody can stop API designers to return multiple possible error responses. Decision is up to them only but in my opinion you should return most relevant error occurred from server side.
There can be many reasons behind it like below :
Unified Error Handling by all clients.
Most relevant error caused will help client more actually as he will not have to take care about all probabilistic error caused.
JSON structure simplicity. ( Error Array will be avoided. )

Jquery JSON FORMAT ERROR

{"simpleSearchResult" :
{
"facultydetail":
[
{"comboValue":"FACULTY"},
{"facultyDetail":
[
"Android",
"Android profile",
"balaguru",
"Ulsoor",
"balaguru#savinirs.com",
"Bangalore",
"Karnataka",
"India",
"83",
"3 years of teaching experiance",
"INR",
"10000.0",
"40",
"20000.0"
]
},
{"NunberOfPages":"1"},
{"CurrentPage":"1"},
{"SkillIds":"an"},
{"cityId":"ba"},
{"DisplayCboValue":"STUDENT"},
{"Limit":"0"},
{"PageComboVal":"10"},
{"discipline":"Languages"}
]
}
}
This is my JSON string from my action class i am returning this to jQuery ajax call but that is always going to error block, gving parse error any one please help me to fix this issue
There are multiple JSON validators available online which can pinpoint the problem with JSON formatting. I used http://www.jsonlint.com/ and it appears that your JSON is valid. The problem is probably somewhere else.