Answer the question
In order to leave comments, you need to log in
How to work with Dadata API?
I want to write a form in Angular with hints from the dadata service. There are two options: use a ready-made solution on their website or write a POST request yourself. I decided that I would write POST myself. There is also a sample code on the site. In general, I get a 400 error from the server - and where I have a problem in the syntax is not clear.
family: "CLIENT_ERROR", reason: "Bad Request", message: "Cannot construct instance of `ru.hflabs.sgt.model.…p.AbstractHTTPDestination$1); line: 1, column: 1]"
export class AppComponent {
suggest: any;
URL = 'https://suggestions.dadata.ru/suggestions/api/4_1/rs/suggest/address';
token = 'f6bf5c998d0e4fcd58cea3b241763e01fe918127';
body = '';
options = {
method: 'POST',
mode: 'cors',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
Authorization: 'Token ' + this.token
},
body: JSON.stringify(this.body)
};
constructor(private suggestService: DadataSuggestService) { }
getSuggestion(postBody): any {
this.body = JSON.stringify(postBody);
this.inputSuggestion();
console.log(postBody);
console.log(this.suggest);
}
inputSuggestion(): void {
this.suggestService.takeSuggestion(this.URL, this.body, this.options)
.subscribe(results => this.suggest = results);
}
}
Answer the question
In order to leave comments, you need to log in
This option works on bare JavaScript:
var dadataToken = ""; // Put your token
var search = ""; // Put your search string
var xhr = new XMLHttpRequest();
xhr.open(
'POST',
'https://suggestions.dadata.ru/suggestions/api/4_1/rs/suggest/address',
true
);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("Authorization", "Token " + dadataToken);
xhr.send(JSON.stringify({"query": search}));
xhr.onload = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
var jsonResponse = JSON.parse(xhr.responseText);
if (jsonResponse.suggestions) {
window.console.log("jsonResponse.suggestions = " + JSON.stringify(jsonResponse.suggestions));
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question