How to post json with check_http

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.
chevb
Posts: 1
Joined: Fri Mar 14, 2014 12:21 pm

How to post json with check_http

Post by chevb »

Hello

Does anyone knows how to post json with check_http plugin. I`m trying to do something like this :

Code: Select all

[root@server01 user01]# /usr/lib64/nagios/plugins/check_http -H serverx.xyz -u /MY-URI --header=content-type:application/json --method=POST --post={"profileId":"anyId","site":"mywebsite","gender":"male","bikeType":"tri","unit":"in","inseam":"10.0","trunk":"10.0","forearm":"10.0","arm":"10.0","thigh":"10.0","lowerLeg":"10.0","sternalNotch":"10.0","overallHeight":"10.0","fitCalcResults":{"aero":{"top_tube_length":"21.9 - 23.9 cm","stem_length":"8 - 10 cm","bb_saddle_position":"22.9 cm","saddle_pedal":"40.1 cm","saddle_ground":"47.9 cm","cranklength":"170 cm","aero_bar_size":"XS","aero_bar_drop_minimal":"-4.2 cm","aero_bar_drop_moderate":"-2.6 cm","aero_bar_drop_intense":"-2 cm","aero_bar_drop_maximal":"-0.8 cm","pad_ground_minimal":"52.1 cm","pad_ground_moderate":"50.5 cm","pad_ground_intense":"49.8 cm","pad_ground_maximal":"48.7 cm"}}}  -t 3 -e 404 -p 80
HTTP CRITICAL - Invalid HTTP response received from host: HTTP/1.1 400 Bad Request
[root@server01 user01]# 
I need to see as result ( -e 404 ) 404 but i`m getting 400 so I think my mistake is related to syntax

Any help is deeply appreciated
abrist
Red Shirt
Posts: 8334
Joined: Thu Nov 15, 2012 1:20 pm

Re: How to post json with check_http

Post by abrist »

Specifying json from the shell is quite an academic task when dealing with escapes, etc. Try posting with a very small bit of json, and then try escaping till you get the expected error, and then fix up your json string with the necessary escapes.

Code: Select all

/usr/lib64/nagios/plugins/check_http -H serverx.xyz -u /MY-URI --header=content-type:application/json --method=POST --post="{\"test\": 0}" -t 3 -e 404 -p 80
You may have to escape ALL quotes in the json string . . .
Former Nagios employee
"It is turtles. All. The. Way. Down. . . .and maybe an elephant or two."
VI VI VI - The editor of the Beast!
Come to the Dark Side.
nshah11
Posts: 8
Joined: Tue Oct 28, 2014 2:51 pm

Re: How to post json with check_http

Post by nshah11 »

Hi,

I am trying to POST JSON string in the check_http command as follows:

/usr/local/nagios/libexec/check_http -H localhost -p 8080 -u /service/storestatus -T "Content-Type:application/json" -k "API_KEY: 0XXX" -j POST --post="{\"store_id\":\"1953\"}" -v

I am trying to escape all the quotes - and tried several things in the --post="<string>" but it does not seem to recognize the JSON sting. I receive the following:

Code: Select all

STATUS: HTTP/1.1 500 Internal Server Error
CONTENT:
{"ack":"1","errors":[{"msg":"REQUEST IS NULL","code":"ERR0004"}],"store_status":null}
Any suggestions would be appreciated. Thanks
sreinhardt
-fno-stack-protector
Posts: 4366
Joined: Mon Nov 19, 2012 12:10 pm

Re: How to post json with check_http

Post by sreinhardt »

How about enclosing it in single quotes and not escaping them? Something like:

Code: Select all

./check_http -H localhost -p 8080 -u /service/storestatus -T "Content-Type:application/json" -k "API_KEY: 0XXX" -j POST --post='{"store_id":"1953"}' -v
Nagios-Plugins maintainer exclusively, unless you have other C language bugs with open-source nagios projects, then I am happy to help! Please pm or use other communication to alert me to issues as I no longer track the forum.
nshah11
Posts: 8
Joined: Tue Oct 28, 2014 2:51 pm

Re: How to post json with check_http

Post by nshah11 »

Thanks for your prompt response sreinhardt.

I tried using the single quote ' ' for the POST string but that gives me the same error. I try the same thing using curl command and it gives me the expected JSON response.

Basically, I want to make sure that the service URL is live and up and monitor the response time - I am fine with using different plugins for each if I have to:
1) For checking if the URL service is up (Post using JSON string)
2) Monitor the response time

Any other suggestions for the check_http POST string syntax? Do you suggest any other plugins based on the use?

Thanks
User avatar
lgroschen
Posts: 384
Joined: Wed Nov 27, 2013 1:17 pm

Re: How to post json with check_http

Post by lgroschen »

Have you accounted for standard htaccess authentication? then '-a username:password' could be required.
/Luke
jraj90
Posts: 7
Joined: Thu Feb 27, 2014 2:27 am

Re: How to post json with check_http

Post by jraj90 »

Hi guys,

I am also facing similar problem trying to post JSON using the check_http plugin.

Did anyone manage to resolve this ? May I know if this is solved.

Thanks

Regards,
- jay -
tmcdonald
Posts: 9117
Joined: Mon Sep 23, 2013 8:40 am

Re: How to post json with check_http

Post by tmcdonald »

Could you give us more specific errors? What is your current check output and how are you running the check?
Former Nagios employee
jraj90
Posts: 7
Joined: Thu Feb 27, 2014 2:27 am

Re: How to post json with check_http

Post by jraj90 »

Hi tmcdonald,

I am performing checks using the check_http plugin to an API service. It requires a POST method sending data in JSON. Here is the requests and respond using check_http.

Check command using the exact JSON Data:-

Code: Select all

./check_http -H test.com -S -k 'Authorization: Bearer fdb6f896xvxvxvxvxvxvx' -u '/smsmessaging/v1/outbound/212830/requests' -j 'POST' -P '{"outboundSMSMessageRequest":{"address":["tel:+8801847111111"],"senderAddress":"tel:12345678","outboundSMSTextMessage":{"message":"Hello World"},"clientCorrelator":"123456","receiptRequest":{"notifyURL":"http://application.example.com/notifications/DeliveryInfoNotification","callbackData":"some-data-useful-to-the-requester"},"senderName":"Test."}}'
Respond:-
HTTP CRITICAL: HTTP/1.1 500 Internal Server Error - 1382 bytes in 0.529 second response time |time=0.528722s;;;0.000000 size=1382B;;;0


Check command by escaping ALL quotes in the json string:-

Code: Select all

./check_http -H test.com -S -k 'Authorization: Bearer fdb6f896xvxvxvxvxvxvx' -u '/smsmessaging/v1/outbound/212830/requests' -j 'POST' -P '{\"outboundSMSMessageRequest\":{\"address\":[\"tel:+8801847111111\"],\"senderAddress\":\"tel:12345678\",\"outboundSMSTextMessage\":{\"message":"Hello World\"},\"clientCorrelator\":\"123456\",\"receiptRequest\":{\"notifyURL\":\"http://application.example.com/notifications/DeliveryInfoNotification\",\"callbackData\":\"some-data-useful-to-the-requester\"},\"senderName\":\"Test.\"}}'
Respond:-
HTTP CRITICAL: HTTP/1.1 500 Internal Server Error - 1382 bytes in 0.573 second response time |time=0.572900s;;;0.000000 size=1382B;;;0


There is also another service which uses GET method, hence no data post required and it works fine

Check command:-
./check_http -H test.com -S -k 'Authorization: Bearer fdb6f896xvxvxvxvxvxvx' -u '/location/v1/queries/location?address=tel%3A%2B8801819210809&requestedAccuracy=1000'

Respond:
HTTP OK: HTTP/1.1 200 OK - 620 bytes in 2.148 second response time |time=2.147742s;;;0.000000 size=620B;;;0


If I tried to post the JSON data using Curl it works fine too. Therefore, can you please guide me how can I post the JSON data.

Thanks

Regards,
- jay -
ssax
Dreams In Code
Posts: 7682
Joined: Wed Feb 11, 2015 12:54 pm

Re: How to post json with check_http

Post by ssax »

Looks like you had some mistakes in your escaping, try this:

http://www.infobyip.com/jsonencoderdecoder.php

If that doesn't work, try this one:

http://codebeautify.org/url-encode-string

Let us know if that works for you.
Locked