Skip to main content
Post a question

Notifications

Community site session details

Community site session details

Session Id : 7k7ml6RG+hUPOCgOpmf1LE
Microsoft Dynamics CRM (Archived)

Web API returning 400 Bad Request

Like (0) ShareShare
ReportReport
Posted on 16 Nov 2017 08:02:08 by 605

Hi All,

I am using Microsoft Dynamics 2016 Online.

I am have a HTML in which I have a javascript which calls a CRM WEB API and filters w.r.t fullname. Here is my code

var searchBoxSelectedValue = window.parent.Xrm.Page.getAttribute("new_name").getValue();

var req = new XMLHttpRequest();

req.open("GET", window.parent.Xrm.Page.context.getClientUrl() + "/api/data/v9.0/contacts?$select=fullname&$filter=fullname eq" + searchBoxSelectedValue, false);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.onreadystatechange = function() {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var results = JSON.parse(this.response);

for (var i = 0; i < results.value.length; i++) {
var mydata = results.value[i]["fullname"];
}
} else {
alert(this.statusText);
}
}
};

req.send(); // error shown in this line

I am getting an error saying "Bad Request" 400. 

req.onreadystatechange = function() {} does not get executed, it gets skipped.

thanks in advance.

Regards,

Rahul

*This post is locked for comments

  • Verified answer
    Rahul G J Profile Picture
    605 on 17 Nov 2017 at 09:29:13
    RE: Web API returning 400 Bad Request

    Hi All,

    I got this problem solved i was erring with single quotes. here is my corrected line:

    req.open("GET", window.parent.Xrm.Page.context.getClientUrl() + "/api/data/v9.0/contacts?$select=fullname&$filter=fullname eq " + "'"+ searchBoxSelectedValue + "'", false);

    Regards,

    Rahul

  • Mahendar Pal Profile Picture
    45,095 on 17 Nov 2017 at 09:17:26
    RE: Web API returning 400 Bad Request

    Have you tried to debug your code and check if query is forming well ?? you can debug js by writing debugger only, check your query while debugging what's is comming or share it here.

    Thanks

  • Rahul G J Profile Picture
    605 on 17 Nov 2017 at 09:10:35
    RE: Web API returning 400 Bad Request

    I tried in CRM rest builder, in there inside code editor i created a variable and tried. Still giving me bad request.

  • Verified answer
    Community Member Profile Picture
    on 16 Nov 2017 at 09:08:34
    RE: Web API returning 400 Bad Request

    Hi,

    you can double check your query with the CRMRestBuilder: github.com/.../CRMRESTBuilder

  • Community Member Profile Picture
    on 16 Nov 2017 at 08:58:40
    RE: Web API returning 400 Bad Request

    I think your URL is not correct. Please check below things:

    1. window.parent.Xrm.Page.context.getClientUrl()  if this is returning the CRM URL.

    2. v9.0 is the correct api version for your environment.

    3. Please give a space between eq and ".

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 293,465 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,586 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans
Loading complete