Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

How to use the API (authorization and POST)

Hi guys!

Sorry for the questions, but I'm new with those kind of things (first year of Computer Science).
The thing I'm trying to do is to add (for example) a category automatically whenever I add a new group in my website, which is in the same server as the Forum, or to sync the profile images between the website and forum.
SSO is already enabled and working, and I can retrieve information with GET requests, but I didn't understand how to make POST requests.

I have 2 situations (and need both working):

1) Form submitted. For example I'm using this script (Javascript):

<script type="text/javascript"> $(document).ready(function() { $("#submit").click(function(event) { event.preventDefault(); var name = $("input#name").val(); var body = $("input#body").val(); var catid = $("input#CategoryID").val(); jQuery.ajax({ type: "POST", url: "http://mywebsite/Forum/api/discussions", contentType: "application/json; charset=utf-8", dataType: "json", data: {'Name': name, 'Body': body, 'CategoryID': catid}, success: function(res) { if (res) { //Success } else { //Error } } }); }); }); </script>
But I'm getting Code:400, API.Error.ContentType.
The Content-Type in the Request Header is: "Content-Type:application/json; charset=UTF-8".

2) Just sending a request via curl (PHP)
$data = array( "Name" => "Test", "Body" => "Test API call", "CategoryID" => "5" ); $data_string = json_encode($data); $ch = curl_init('http://mywebsite/Forum/api/discussions'); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen($data_string)) ); $result = curl_exec($ch); $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $contenttype = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); print "Status: $httpcode" . "\n"; print "Content-Type: $contenttype" . "\n"; print "\n" . $result . "\n";

But in this case I'm getting Code: 401, API.Error.AuthRequired, because I'm not sendind the cookies.

I tried everything with Postman (like the tutorial that i found in this Forum, but with session auth), and I'm getting a third error: Code: 400,
"Corpo è richiesto. Categoria è richiesto. Nome è richiesto." (=Body required. Category required. Name required.).

Can you please help me?
I tried to figure it out by myself, but my knowledge about those things it's zero and google didn't helped.

Thank you!


Sign In or Register to comment.