Forum Discussion

fuenteso's avatar
fuenteso
Leader
9 years ago

Using HTTP PUT on an endpoint

Hi!

 

I've been trying to figure out how to edit a post directly from an endpoint. I not quite familiar with HTTP request over javascript, and the documentation (Updating message using HTTP PUT) is not very clear to me.

 

I've tried a couple of things with jQuery, but it looks like jQuery is not available on endpoints. I also tried pure javascript (XMLHttpRequest) but I'm not getting any results.

 

This is what I used:

<script type="text/javascript" language="JavaScript">
var data = {"data": {"type": "message","subject": "hello world","body": "this is the edited message post."}};
xhr = new XMLHttpRequest();

xhr.open('POST',encodeURI('http://api.lithium.com/community/2.0/<mycommunityID>/messages/5909'));
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.onreadystatechange = function(aEvt) {
    if (xhr.readyState == 4) {
     if(xhr.status == 200)
      console.log(xhr.responseText);
     else
      console.log("Error loading page\n");
  }
};

xhr.send(encodeURI('data=' + data));

</script>

And I got this error:

"XMLHttpRequest cannot load http://api.lithium.com/community/2.0/psg/messages/5909. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '<my stage URL>' is therefore not allowed access. The response had HTTP status code 503."

 

 

Anyone has any examples?

 

I would appreciate any help, Thanks!

  • DougS's avatar
    DougS
    9 years ago

    I meant you need to use an endpoint that is hosted outside of the community to make the back-channel access token request. We currently restrict endpoints from calling .lithium.com domains for security reasons.

     

    If you are not able to host your own endpoint to make the access token request, you could try hitting the REST v1 messages/id/[message-id]/edit endpoint directly on the community (not through api.lithium.com) to make your message edit.

     

    -Doug

11 Replies

  • fuenteso's avatar
    fuenteso
    Leader
    9 years ago

    Yes, I'm doing some role checking before executing anything, but I guess you're right, no need to use restadmin if I'm already checking the role and making sure the user is an admin.

     

    Thanks!