I'm trying to use the 'json' validation rule in a form request, but I constantly get an invalid JSON message. I havent found any examples on how to use this rule, so I'm a bit lost here.
My rules function looks like this:
public function rules()
{
return [
'userData'=>'json',
'securityChanges'=>'json'
]
}
And my JSON looks like this:
{
"userData":{
"domicilio":"nowere",
"empresa":"Burgerking",
"name":"zorgito",
"surname": "perez",
"cuit":"10101010"
},
"securityChanges":{
"email": "flasheadas#lolo.com",
"password": "777777777777",
"passwordConfirmation":"777777777777"
}
}
My headers are properly set:
Accept: application/json
Content-Type: application/json
It should work, but I get:
"The user data must be a valid JSON string."
Any idea what's wrong here?
EDIT:
as requested n one of the answers, I'm putting the output of a dd($this->all()); in my form request class
array:2 [
"userData" => array:5 [
"domicilio" => "nowere"
"empresa" => "Burgerking"
"name" => "zorgito"
"surname" => "perez"
"cuit" => "10101010"
]
"securityChanges" => array:3 [
"email" => "flasheaasddas#lolo.com"
"password" => "777777777777"
"passwordConfirmation" => "777777777777"
]
]
The whole HTTP request is:
PATCH /api/users/me HTTP/1.1
Host: DOMAIN REMOVED
Authorization: Bearer (TOKEN REMOVED.)
Accept: application/json
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: 73516b63-8556-4c21-9050-7a4fa3a83cb1
{
"userData":{
"domicilio":"nowere",
"empresa":"Burgerking",
"name":"zorgito",
"surname": "perez",
"cuit":"10101010"
},
"securityChanges":{
"email": "flasheaasddas#lolo.com",
"password": "777777777777",
"passwordConfirmation":"777777777777"
}
}
Nothing clear here, please make dd($request->all()) or dd($this->all()) (if you are in request class directly) and make sure you have that fields, and then make sure that fields are valid json strings. You can check it here https://jsonlint.com/
Related
When I make a GET http request for the metadataHeaders that only requests one of them like so:
https://gmail.googleapis.com/gmail/v1/users/me/messages/${messageId}?$format=metadata&metadataHeaders=From
it works just fine. But my question is how do I go about sending the array of headers that I want [to, from, subject], in my request? So basically, how would I restructure my metadataHeaders query parameter... found here ->
https://gmail.googleapis.com/gmail/v1/users/me/messages/${messageId}?$format=metadata&metadataHeaders=From
to also contain From, To, & Subject
I have been trying to figure out how to get these headers for quite a while to no avail. I tried looking at the documentation ( https://developers.google.com/gmail/api/reference/rest/v1/users.messages/get ) but although I know its possible thanks to it, I can't seem to find out how to implement it in my http request. I also tried looking through the stack overflow responses to similar questions but many weren't really useful at all since many of the questions were different from mine, using the oauth library, or in a programming language. All I care for is how to make the http request.
Headers is just an array So you can just add it more then once
metadataHeaders=from&metadataHeaders=to&metadataHeaders=subject
Request:
GET https://gmail.googleapis.com/gmail/v1/users/me/messages/185cf8d12166fc7a?format=metadata&metadataHeaders=from&metadataHeaders=to&key=[YOUR_API_KEY] HTTP/1.1
Authorization: Bearer [YOUR_ACCESS_TOKEN]
Accept: application/json
Resonse:
"payload": {
"partId": "",
"headers": [
{
"name": "From",
"value": "[REDACTED]"
},
{
"name": "To",
"value": "[REDACTED]#gmail.com"
},
{
"name": "Subject",
"value": "Security alert"
},
]
},
There is something strange going on with one of my Karate tests. I have quite a few I've inherited (rather than written myself) and they mostly all work fine. My application uses GraphQL and I'm changing an existing mutation function to add an argument. The new argument is a list of DestinationInput defined like this:
createScheduledTitles(titles: [ScheduledTitleInput!]!, destinations: [DestinationInput]!): [ScheduledTitle!]!
This is how I set up my arguments in my Karate script:
* def input =
"""
{titles: [ {
...
],
destinations: [
{
'destinationType': "od",
'brightcovePublish': "true"
}
]
}
"""
This gets rejected by the server end (java) with:
The variables input contains a field name 'stinationType' that is not defined for input object type 'DestinationInput'
Well, yes, of course but I'm supplying 'destinationType' not 'stinationType'. When I check the log of what was sent though I see this:
"destinations": [
{
"stinationType": "od",
"ightcovePublish": "true"
}
]
Huh? Where'd those initial characters go? I've since tried every combination of quotes (double, single, none) on field names and arguments. I've tried different line formatting etc. Some slight differences, some combinations leave the quotes in the field name so I see stinationtype\' as the field name in error, and sometimes it chews more letters off the front. I've retyped the field names in case I'd edited some hidden chars in there. So far nothing gets me to success. I've other working examples of this kind of thing and I've copied their format, but still no joy. Whatever is happening is at the client end because the log is showing the bad field names as being sent. Anyone know what I'm doing wrong?
Dev of Karate here - this is weird, never seen anything like it. Do keep in mind this may be a genuine server-side bug - but I guess you have looked at the request / http log.
My other guess without seeing the whole project structure is there may be some custom Java / JS code that is pre-processing the request, common for GraphQL projects. Try forming a cURL request that works, step 2 is convert that to a simple (hard-coded) karate test. and if you can manage this process, we can look at it: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue
But I just tried using your sample JSON in a test you can replicate (even the HTTP POST). So try pasting this into a fresh Scenario:
Scenario:
* def input =
"""
{titles: [
{}
],
destinations: [
{
'destinationType': "od",
'brightcovePublish': "true"
}
]
}
"""
* print input
* url 'https://httpbin.org/post'
* request input
* method post
Which results in:
14:26:59.370 [main] INFO com.intuit.karate - [print] {
"titles": [
{
}
],
"destinations": [
{
"destinationType": "od",
"brightcovePublish": "true"
}
]
}
14:26:59.614 [main] DEBUG com.intuit.karate - request:
1 > POST https://httpbin.org/post
1 > Accept-Encoding: gzip,deflate
1 > Connection: Keep-Alive
1 > Content-Length: 84
1 > Content-Type: application/json; charset=UTF-8
1 > Host: httpbin.org
1 > User-Agent: Apache-HttpClient/4.5.11 (Java/1.8.0_231)
{"titles":[{}],"destinations":[{"destinationType":"od","brightcovePublish":"true"}]}
14:27:00.890 [main] DEBUG com.intuit.karate - response time in milliseconds: 1274.99
1 < 200
1 < Access-Control-Allow-Credentials: true
1 < Access-Control-Allow-Origin: *
1 < Connection: keep-alive
1 < Content-Length: 696
1 < Content-Type: application/json
1 < Date: Thu, 13 Feb 2020 08:57:00 GMT
1 < Server: gunicorn/19.9.0
{
"args": {},
"data": "{\"titles\":[{}],\"destinations\":[{\"destinationType\":\"od\",\"brightcovePublish\":\"true\"}]}",
"files": {},
"form": {},
"headers": {
"Accept-Encoding": "gzip,deflate",
"Content-Length": "84",
"Content-Type": "application/json; charset=UTF-8",
"Host": "httpbin.org",
"User-Agent": "Apache-HttpClient/4.5.11 (Java/1.8.0_231)",
"X-Amzn-Trace-Id": "Root=1-5e450f5c-18d2618834da3a7afdd3402a"
},
"json": {
"destinations": [
{
"brightcovePublish": "true",
"destinationType": "od"
}
],
"titles": [
{}
]
},
"origin": "103.15.250.10",
"url": "https://httpbin.org/post"
}
I would like to Parse tomcat logs where we have soap/rest request and response. can any one give me any good example where we can parse those logs and save it in elastic search in json format.
filter {
if [type] == "tomcatlog"{
multiline {
#type => "all" # no type means for all inputs
pattern => "^%{TIMESTAMP_ISO8601}"
negate => true
what => "previous"
}
grok {
match => {
message => "%{TIMESTAMP_ISO8601:timestamp}%{SPACE}\{(?<thread>[^)]+)\}%{SPACE}%{LOGLEVEL:level}%{SPACE}\[(?<logger>[^\]]+)\]%{SPACE}%{SPACE}%{GREEDYDATA:message}"
}
}
date {
match => [ "timestamp", "yyyy-MM-dd HH:mm:ss,SSS" ]
remove_field => [ "timestamp" ]
}
}
}
Use http://grokdebug.herokuapp.com/ to create grok filters. Useful pattern are listed patterns
Thanks Allen for your response. Here is my example which I am trying to parse using Grok Pattern. I am new to this one so trying to figure it out whether this one is right approach or not.
2015-09-28 10:50:30,249 {http-apr-8080-exec-4} INFO [org.apache.cxf.services.interfaceType] 1.0.0-LOCAL - Inbound Message
ID: 1
Address: http://localhost:8080/interface/interface?wsdl
Encoding: UTF-8
Http-Method: POST
Content-Type: text/xml; charset=UTF-8
Headers: {Accept=[/], cache-control=[no-cache], connection=[keep-alive], Content-Length=[2871], content-type=[text/xml; charset=UTF-8], host=[localhost:8080], pragma=[no-cache], SOAPAction=["http://services.localhost.com/calculate"], user-agent=[Apache CXF 2.7.5]}
Payload: users_19911111test123456false
I have a json object that I'm sending to Google's QXP Express API. The idea is that I send the object with the relevant travel information. In terminal, through curl, it's very easy to send it. I just use the following curl command. Doc.json is the file name of the json.
curl -d #doc.json --header "Content-Type: application/json" https://www.googleapis.com/qpxExpress/v1/trips/search?key=AIzaSyAaLHEBBLCI4aHLNu2jHiiAQGDbCunBQX0
This is my code to do it in Ruby.
uri = URI('https://www.googleapis.com/qpxExpress/v1/trips/search?key=MYAPIKEY')
req = Net::HTTP::Post.new uri.path
req.body = {
"request" => {
"passengers" => {
"adultCount" => 1
},
"slice" => [
{
"origin" => "BOS",
"destination" => "LAX",
"date" => "2014-10-14"
},
{
"origin" => "LAX",
"destination" => "BOS",
"date" => "2014-11-14"
}
]
}
}.to_json
res = Net::HTTP.start(uri.host, uri.port, :use_ssl => true) do |http|
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http.ssl_version = :SSLv3
http.request req
end
puts res.body
However I'm getting back the following error.
{
"error": {
"errors": [
{
"domain": "global",
"reason": "parseError",
"message": "This API does not support parsing form-encoded input."
}
],
"code": 400,
"message": "This API does not support parsing form-encoded input."
}
}
I just need to send it with the json file, but nothing I can find online covers sending json's to APIs. Please help, I'm very stuck.
It is always matter of taste what tools you prefer, but as for me i am currently using the rest-client gem for accessing REST APIs. With this library your example could be written like this:
require 'json'
require 'rest-client'
response = RestClient.post 'https://www.googleapis.com/qpxExpress/v1/trips/search?key=AIzaSyAaLHEBBLCI4aHLNu2jHiiAQGDbCunBQX0',
{
request: {
passengers: {
adultCount: 1
},
slice: [
{
origin: "BOS",
destination: "LAX",
date: "2014-10-14"
},
{
origin: "LAX",
destination: "BOS",
date: "2014-11-14"
}
]
}
}.to_json,
:content_type => :json
puts response.body
But if you want a Net::HTTP only solution, this might not be a suitable answer for you.
I'm trying to check my api implementation with my documentation written in blueprint. I've expected that dredd will fail when json returned from server will be different than specified in documentation. To check this I've copied dredd-example. First I've run dredd with original apib file to make sure that all is green. Then I've modified response in documentation and expected dredd to show me some red... But it doesn't.... it looks like tool is only checking response headers but not the response body. Here is output from console:
pass: GET /machines duration: 18ms
request:
host: localhost
port: 3000
path: /machines
method: GET
headers:
User-Agent: Dredd/0.2.1 (Darwin 13.0.0; x64)
expected:
headers:
Content-Type: application/json
body:
[
{
"_id": "52341870ed55224b15ff07ef",
"type": "bulldozer",
"name": "willyxxxxxx" #HERE IS WHERE I CHANGED RESPONSE IN DOCUMENTATION
}
]
status: 200
actual:
headers:
x-powered-by: Express
content-type: application/json
content-length: 95
date: Thu, 20 Mar 2014 08:22:40 GMT
connection: keep-alive
body:
[
{
"_id": "532aa5507dcdfff362931799",
"type": "bulldozer",
"name": "willy"
}
]
status: 200
Can I check response body using dredd? And how can I do this?
In JSON bodies Dredd is checking only for keys not for values. When you change key in the expected JSON body document, it will definitely fails.