Itzik Gili
Itzik Gili

Reputation: 324

Why does AngularJS add colon in the end of data object in $http post request

When trying to use angular js $http to post a request to elasticSearch I get an "Unexpected token : " Error.

My code looks like this:

var request= $http({
    method: "post",
    url: path,
    accept:"*/*",
    headers:{"Content-Type" : "application/x-www-form-urlencoded; charset: UTF-8"},
    data:{
         "query":{
               "fuzzy":{
                    "title":{
                        "value": $scope.searchTerm,
                        "fuzziness":"1"
                    }
                }
        },
        "highlight":{
            "fields":{
                "*":{}
            }
        }
   }
});

When looking in the form data section on chrome console I see the json with a trailing colon.

[{"query":{"fuzzy":{"title":{"value": $scope.searchTerm,"fuzziness":"1"}}},
"highlight":{"fields":{"*":{}}}}]:    <--- this is the problem

That is strange. Any ideas on how to eliminate the trailing colon?

Upvotes: 1

Views: 978

Answers (2)

Kalle
Kalle

Reputation: 452

In our case it was that the http.post was missing HTTP header "content type" that should be set to "application/json", it that is was is posted.

If your posting json, just add

{headers:{'Content-Type': 'application/json'}}

as third parameter of the post method. So it is

$http.post( endpoint, json_payload, {headers:{'Content-Type': 'application/json'}} )

Upvotes: 0

Itzik Gili
Itzik Gili

Reputation: 324

For anyone who encounter this kind of behavior,

In my case it was because I indexed a document with wrong JSON structure. When using the bulk indexing option with elasticSearch - JSONs with invalid structure are indexed without warning.

The error was actually in the response , not on the http request.

Try re-indexing the document which will probably fix this issue.

Upvotes: 1

Related Questions