Support

Home Forums Event Espresso Premium POST request JSON API

POST request JSON API

Posted: April 9, 2014 at 4:43 pm

Viewing 6 reply threads


Joey Duguay

April 9, 2014 at 4:43 pm

Hello all,

first, I’d like to say that I’m a student and I use Esspresso JSON API (v2.1.1.P) for three days, so I want you to excuse me if the answer to my question is obvious.

I only want to do a POST request to modify a registration.

First, I get what I want to change like this :

url = "{my_website}/espresso-api/v1/registrations/{session_key}.json?Registration.code__like={a_registration_code}";

$.get(url, function(data) {

});

After, in the GET callback function, I want to modify the json and to POST it :


$.get(url, function(data) {
body = data.body;
/* Here I will do some modifications to body */
$.post(url, body, function(data) { // Here I send back to the same URL
console.log(data);
});
});

But I have this error : “POST and PUT requests must contain all input in a field called ‘body’. Eg: ‘body:”{\’Registrations\’:[{\’id:\’12,…}]}”

I don’t understand because for me I send something like this.

So :
– Is it correct to make POST request to this kind of URL ?
– What is not correct in the “body” object I send ?

Thank you,

Tony


Dean

April 10, 2014 at 4:13 am

Hi Tony,

I’ll be honest and say that my JSON API knowledge isn’t the best, but if I am understanding this correctly it appears that the body variables format is incorrect, in that your info has the container data rather than body. See here https://eventespresso.com/wiki/api-addon/#POST.2FPUT_.2Fregistrations


Joey Duguay

April 10, 2014 at 7:08 am

Thank you for your answer.

If I do this I don’t have the error :

<pre class=”brush: javascript; gutter: true; first-line: 1; highlight: []; html-script: false”>$.get(url, function (data) {

datas = {body : data.body};

$.post(url, datas, function (data) {
console.log(data);
});

});

But I have another error that I don’t understand because datas is an object :

Warning: json_decode() expects parameter 1 to be string, array given in /home/kimantis/public_html/dev/wp-content/plugins/espresso-json-api/includes/helpers/EspressoAPI_Response_Formatter.class.php on line 95

Warning: Cannot modify header information – headers already sent by (output started at /home/kimantis/public_html/dev/wp-content/plugins/espresso-json-api/includes/helpers/EspressoAPI_Response_Formatter.class.php:95) in /home/kimantis/public_html/dev/wp-content/plugins/espresso-json-api/includes/helpers/EspressoAPI_Response_Formatter.class.php on line 47
{“status”:”Registration in wrong Event Espresso Format! Expected value: Registrations but it wasnt set in . “,”status_code”:500}


Joey Duguay

April 10, 2014 at 6:30 pm

So, nobody does POST request using JSON API ?

I’m on it for two days now, and I’m sure a basic example will show me what I do bad.


Dean

April 11, 2014 at 1:11 am

Hi,

“Warning: json_decode() expects parameter 1 to be string, array given ”

Well, json_decode requires a string not an array and an object is not a string.

I’ll put this thread under the nose of the JSON API developer to see if he can provide a clearer example than I can.


Michael Nelson

  • Support Staff

April 11, 2014 at 12:24 pm

you almost have it right there Joey.

The JSON api expects you to send a POST with an argument named ‘body’, which contains a string of JSON data. However, when you are calling jQuery.post(url,{body:body_var},callback), where body_var is a javascript object, jQuery is converting it into a multi-dimensional array of POST arguments.

If you use a web-inspector (like FireBug for firefox) you can see the AJAX request going out. You were sending one similar to this:
https://drive.google.com/file/d/0B5P8GXTvZgfMdnVmUFYzam4zcjQ/edit?usp=sharing
When you should have been sending something like this:
https://drive.google.com/file/d/0B5P8GXTvZgfMM3ZoMW1LV1dPMHc/edit?usp=sharing

To do that, all you need to do is convert the data.body from a javascript object into a JSON string. One way to do that is to call JSON.stringify(data.body). See this example:

url = "http://localhost/eetrunk31/espresso-api/v1/registrations/9gl23pxqfz.pretty_json";
	jQuery.ajax(url,{
		cache:true,
		success:function(data){
			console.log(data);
			jQuery.post(url,
			{body: JSON.stringify(data.body)},
			function(data2){
				console.log(data2)
			})
		}
	})

Here is a stack overflow discussion on other ways to convert a javascript object into JSON string, and the downside of using JSON.stringify(): http://stackoverflow.com/questions/4162749/convert-js-object-to-json-string

Also, we haven’t provided any examples on how to send data to the JSON api using javascript because this is the type of thing normally only site-admins should be doing. If you’re using this javascript code from a publicly-accessible page, realize that anyone can load the page and CHANGE your javascript to do whatever they want! So make sure you’re either only using this javascript from a password-protected page, or from server-side code which isn’t visible to site visitors.


Joey Duguay

April 11, 2014 at 4:00 pm

You’re right, I finally decided to do not do that.
I wanted to upload additional informations using JSON, but it’s not made for that and it’d be insecure.

What I’m going to do is using custom questions, and I will bring the answers by SQL requests because JSON doesn’t provide answers for attendees additional questions.

It’s no longer the topic, but you can suggest your ideas to do that better.

Thanks


Sidney Harrell

April 18, 2014 at 12:21 pm

The answers table (wp_events_answer) is pretty simple. We recommend using the WPDB object to interact with it (). You’ll need the attendee’s registration_id, attendee_id, and the question_id before you do the insert. Also make sure that the question is part of a group that is assigned to additional attendees for that event. So it would look something like:

$wpdb->insert( EVENTS_ANSWER_TABLE, array( 'registration_id' => $registration_id, 'attendee_id' => $attendee_id, 'question_id' => $question_id, 'answer' => $answer), array( '%s', '%d', '%d', '%s'));
Viewing 6 reply threads

The support post ‘POST request JSON API’ is closed to new replies.

Have a question about this support post? Create a new support post in our support forums and include a link to this existing support post so we can help you.

Event Espresso