utilizing axios encoding settings within vue.js

I am currently utilizing vue.js along with axios to retrieve data from an API endpoint.

The specific URL I require is structured like this:

https://base_url.com?f=json&where=&returnGeometry=false&spatialRel=esriSpatialRelIntersects&geometry={"rings":[[[346564.4406999983,6675729.9070999995],[346507.04410000145,6675711.315700002],[346501.2646000013,6675723.884199999],[346495.0234000012,6675737.456500001],[346555.5135000013,6675757.049900003],[346555.91690000147,6675757.180699997],[346555.92969999835,6675757.141800001],[346557.9803000018,6675750.865099996],[346559.90100000054,6675744.985399999],[346560.7393000014,6675742.419500001],[346564.2879999988,6675731.5568],[346564.4406999983,6675729.907099999...

However, when using axios, the parameters get transformed into a different format:

?f=json&where=&returnGeometry=false&spatialRel=esriSpatialRelIntersects
&geometry=%257B%2522rings%2522%253A%255B%255B%255B346564.4406999983%252C6675729.9070999995%255D%252C%255B346507.04410000145%252C6675711.315700002%255D%252C%255B346501.2646000013%252C6675723.884199999%255D%252C%255B346495.0234000012%252C6675737.456500001%255D%252C%255B346555.5135000013%252C6675757.049900003%255D%252C%255B346555.91690000147%252C6675757.180699997%255D%252C%255B346555.92969999835%252C6675757.141800001%255D%252C%255B346557.9803000018%252C6675750.865099996%255D%252C%255B346559.90100000054%252C6675744...

The API endpoint, on the other hand, expects the parameters in this format:

f=json&where=&returnGeometry=false&spatialRel=esriSpatialRelIntersects&geometry=%7B%22rings%22%3A%5B%5B%5B346564.4406999983%2C6675729.9070999995%5D%2C%5B346507.04410000145%2C6675711.315700002%5D%2C%5B346501.2646000013%2C6675723.884199999%5D%2C%5B346495.0234000012%2C6675737.456500001%5D%2C%5B346555.5135000013%2C6675757.049900003%5D%2C%5B346555.91690000147%2C6675757.180699997%5D%2C%5B346555.92969999835%2C6675757.141800001%5D%2C%5B346557.9803000018%2C6675750.865099996%5D%2C%5B346559...

It appears that the encoding for the geometry parameter is incorrect or differs from what the API requires.

Here is the snippet of my code:

return axios
            .get(process.env.VUE_APP_WATERINFO_RISK_URL, {
                params: {
                    f: 'json',
                    where: '',
                    returnGeometry: false,
                    spatialRel: 'esriSpatialRelIntersects',
                    geometry:
                    {
                        "rings": [[[346564.4406999983, 6675729.9070999995], [346507.04410000145, 6675711.315700002], [346501.2646000013, 6675723.884199999], [346495.0234000012, 6675737.456500001], [346555.5135000013, 6675757.049900003], [346555.91690000147, 6675757.180699997], [346555.92969999835, 6675757.141800001], [346557.9803000018, 6675750.865099996], [346559.90100000054, 6675744.985399999], [346560.7393000014, 6675742.419500001], [346564.2879999988, 6675731.5568], [346564.4406999983, 6675729.9070999995]]],
                        "spatialReference": { "wkid": 102100, "latestWkid": 3857 }
                    }
                    ,
                    geometryType: 'esriGeometryPolygon',
                    inSR: '102100',
                    outFields: '*',
                    outSR: '102100',
                    callback: ''
                }
            })
            .then(
                response => {
                    console.log(response);
                })
            .catch(error => {
                console.log(error);
            });

What steps should I take to ensure my data/url conforms to the proper format for the endpoint?

Thank you in advance :)

Answer №1

here is a sample code snippet:

const axios = require('axios')


const params = new URLSearchParams()
params.append('name', 'John Doe')
params.append('age', '30')
params.append('position', 'Web Developer')
params.append('description', 'skills=HTML%2CCSS%2CJavaScript&experience=5+ years&portfolio=https://www.johndoe.com/')
params.append('active', true)

const config = {
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded'
  }
}

axios.post(url, params, config)
  .then((result) => {
    // Do something with the result
  })
  .catch((err) => {
    // Handle errors
  })

Alternatively, you can define options and pass them as parameters:

customAPICall(){
const options = {
                    format: 'json',
                    region: 'US',
                    population: '>1000000'
};
API.doCustomCall(options).then(response => {
console.log(response)});
}

Then make the API call using the defined options:

doCustomCall(options) {
return axios.get(process.env.CUSTOM_API_ENDPOINT, {Params: options});
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Achieving JSX rendering in Vue.js with TypeScript starting from a basic CLI setup along with the JSX package integration

The Setup I have set up a new project using the vue-cli, where I manually selected certain features including Babel, TypeScript, Vuex, and Linter / Formatter. Additionally, I chose version 2.x and opted to use Babel alongside TypeScript for modern mode an ...

Pause the event listener temporarily and reattach it after specific actions have been completed

I am currently working on a React app that includes a scrollable div. Here is an example: <main id="main" onScroll={this.handleScroll}> My question is, how can I temporarily remove the onScroll event listener from the main element and then reattach ...

Press the button to automatically scroll to a designated div section

One of the challenges I'm facing involves a fixed menu and content box (div) on a webpage. Whenever I click on the menu, the content box should scroll to a specific div. Initially, everything was working fine. Here is a sample of it in action: http ...

Is it advisable to load 10,000 rows into memory within my Angular application?

Currently, I am in the process of creating a customer management tool using Angular.js that will allow me to directly load 10,000 customers into the $scope. This enables me to efficiently search for specific data and manipulate it without the need for serv ...

ensure that angular's ng-if directive sets aside space to display content when triggered by an event

I'm facing an issue with my modal form setup where if a text-field is left blank, a label saying "it is required" is generated below the field, causing the "Proceed" button to move (along with all other elements below it). How can I make sure that DOM ...

A guide on scheduling automatic data insertion in mongoose.js

I'm working on a website with Node.js and Mongoose, and I could use some guidance on how to automatically insert data into Mongoose with Node.js at specific times. For example, at the end of each month, I want my current data to be stored in the Mongo ...

Real-time monitoring within a callback function in Angular 5

I need to ensure that a specific callback is executed only after receiving a response, starting from the line this.groupDefaultExpanded = -1; onwards. loadLoginDetails() { this.derivativeSpecService.getDerivativeDetails().subscribe( res => ...

What is the best way to determine the dimensions of a KonvaJs Stage in order to correctly pass them as the height/width parameters for the toImage function

Currently, I am using KonvaJs version 3.2.4 to work with the toImage function of the Stage Class. It seems that by default, toImage() only captures an image of the visible stage area. This is why there is a need to provide starting coordinates, height, and ...

Filter the object by its unique identifier and locate the entry with the highest score

I'm currently working on figuring out the proper syntax for sorting an object by ID and then identifying the highest score within that ID array. While I've managed to sort the object up to this point using conditionals and the sort() method, I&ap ...

How to properly format credit card input using JavaScript or jQuery

I need to implement a feature where users input their credit card information, and once they complete the form, I want to display the credit card number in this format: xxxxxxxxxxxx1111 or xxxx-xxxx-xxxx-1111, without altering the actual input value. Is ...

I'm having trouble modifying the backdrop to 'true' or removing it after setting it to 'static' in Bootstrap. Can anyone help me troubleshoot this issue?

I have been encountering an issue with changing the backdrop setting from 'static' to 'true' in Bootstrap modal. Here is the code I am using: $('#modal').modal({backdrop: 'static', keyboard: false, show: true}); ...

Using ReactJS and Ruby on Rails to implement an AJAX delete request

There is a component named Items residing inside a parent component called ItemsContainer. A click on a button within the Items component triggers an Ajax function to delete that specific Item. However, I am encountering a 500 error message at the moment ...

What is preventing me from updating the value of a variable in this manner?

Trying to understand the reason behind an issue with overwriting a value passed to an angularJS directive using an isolate scope (@). When attempting to change the value of vm.index with the following: vm.index = parseInt(vm.index, 10) It does not seem t ...

Is it possible to replicate the functionality of "npm run x" without including a "scripts" entry?

If you want to run a node command within the "context" of your installed node_modules, one way to do it is by adding an entry in the scripts field of your package.json. For example: ... "scripts": { "test": "mocha --recursive test/**/*.js --compiler ...

Conceal the ion-tabs in an ionic framework

I have been implementing a solution to hide ion-tabs by following this codepen example: <ion-tabs ng-class="{'tabs-item-hide': hideTabs}"> // --> my tabs content </ion-tabs> <ion-view hide-tabs> // --> my content ...

The deep linking feature may fail to function properly when using a redirect URL

Hello there! So I've been using Capacitor with VueJS and everything is running smoothly, except for one small hiccup. My deep link functions perfectly fine, but it seems to hit a roadblock when it's a redirect URL. My goal is to integrate oauth2 ...

What is the best way to create a modal popup window using Vuejs?

<style src="./LoginButton.scss" lang="scss"></style> <i18n src="./LoginButton.txt"></i18n> <script src="./LoginButton.js"></script> <template> <div class="header-login component-same ml-10"> <span v ...

Creating a Discord Bot: Building an Embed Table/List with Segmented Sections

Currently in the process of learning Javascript and venturing into discord.js, I'm fully aware that the code I am working with is not up to par and requires some serious refinements. The main objective here is to split up the arguments of a command a ...

Retrieve the value of a property in a JavaScript object by specifying a dynamic key for the property

As I work on my current project, I find myself immersed in a world of SVG animations. The challenge lies in triggering these animations as the user scrolls down to view the SVGs. To address this, I took the approach of creating functions for each Snap.SVG ...

Oops! The program encountered an error where it cannot assign a value to the 'id' property of an undefined object while trying to store a session in redis

I am currently working on an Angular4 login application and I am looking to store sessions in Redis. To accomplish this, I am utilizing Express session. However, I have encountered the following error: req.session.id = userName; ...