Can't POST from App to Slack Incoming Webhook

I can POST my nested webhook using curl:

curl -X POST -H 'Content-type: application/json' --data '{"text":"Hello world"}' https://hooks.slack.com/services/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

      

But when I use

$.ajax({
    url: "https://hooks.slack.com/services/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",
    data: '{"text": "Hello world"}',
    type: "POST",
    contentType: "application/json"
})
    .done(function (reply) {
        console.log("POST to Slack succeeded")
    })
    .fail(function (xhr, status, errorThrown) {
        console.log("Error in POST to Slack: " + errorThrown.toString())
    })

      

on the page of my app which is served by localhost: 8090, I get the error:

jquery.js:9536 OPTIONS https://hooks.slack.com/services/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 
400 (Bad Request)
XMLHttpRequest cannot load https://hooks.slack.com/services/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. 
Response for preflight has invalid HTTP status code 400

      

Usually a POST from my page goes to my server which was serving the page. So I think my confusion has to do with understanding how Webhooks work. I thought the policy of the same origin prevented me from POSTing to an arbitrary url. But why does curl command work, and how do I get my page to do what curl does?

Do I need to send POST from my server and not from the client browser?

+3


source to share


1 answer


I got a response from Ben J at Slack. All I had to do was delete the line

contentType: "application/json"

      



Great, quick help from the Slack team. Much appreciated.

+5


source







All Articles