The AngularJS $resource is taking the entire object and embedding it into the URL

I am currently utilizing Angular version 1.4.8 along with Angular UI and TypeScript in my project. The models are defined as follows:

export interface IBatch extends ng.resource.IResource<IBatch> {
    id: Number;
    ...
}

export interface IBatchResource extends ng.resource.IResourceClass<IBatch> {
    snapshot(batch: IBatch);
}

I have configured my Batch resource to include a custom HTTP verb TAKE-SNAPSHOT, which can return either a 200 OK or a 404 NOT FOUND:

var paramDefaults = {
    id: '@id'
};

var actions = {
    'snapshot': { method: 'TAKE-SNAPSHOT' }
};

return <IBatchResource> this.$resource('/api/batches/:id', paramDefaults, actions);

This setup allows me to take a snapshot of a specific Batch by only providing the batch Id as a parameter for the API call. However, I've noticed that when using $resource, it encodes the entire Batch object into the query string, resulting in a very long URL (the actual length is over 1000 characters, but shortened here for clarity):

localhost:15000/api/batches/4?$originalData=%7B%22id%22:4,%22createdDateUtc%22:%222015-12-...

I am seeking guidance on how to direct the $resource request specifically to localhost:15000/api/batches/4.

Answer №1

Here's how I found a solution:

const defaults = {
    id: '@id'
};

const actions = {
    'snapshot': <ActionDescriptor>{ method: 'TAKE-SNAPSHOT' }
};

const result = <IBatchResource> this.$resource('/api/batches/:id', defaults, actions);

/*
 * When passing an object, the entire Batch gets serialized into the URL query string.
 * To only pass the ID, we need to make adjustments as shown below.
 */
const oldFn = result.snapshot;
result.snapshot = (batch: IBatch) => oldFn(<any>{id: batch.id });
return result;

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

Are you looking to create a dynamic quiz platform with Angular?

Within my program, there are two choices available: one is to host the quiz and the other is to join the quiz. Upon hosting a quiz, a random code will be created and must be shared so that participants can join the quiz. Which Angular concept should be u ...

Streaming RTMP video through a WebView

Currently, I am working on implementing rtmp streaming using WebView. To achieve this, I referred to the code provided on Stack Overflow under this link The easiest way to play an audio RTMP stream in Android, which was shared by a user named Club. After a ...

Validator returns undefined when expressing invalid data

Having an issue with validation, here is the code snippet: routes.js var express = require('express'); var router = express.Router(); var hello_controller = require('../api/controllers/helloController'); var { validationRules, validat ...

Set up SystemJS to properly load my Angular 2 component module

Recently, I made the switch from ng1 to ng2. I successfully imported Angular 2 and its modules into my project: <script src="/node_modules/systemjs/dist/system.src.js"></script> <script src="/node_modules/rxjs/bundles/Rx.js"></script& ...

Execute the function right away and then at regular intervals of X seconds

Need help with Angular 7 function call timing checkData(): Observable<string> { return this.http.get('') .pipe( map(res => { let result; result = { packageNumbe ...

Accessing the transcluded scope of a directive from its preLink method: A beginner's guide

Recently, I have come to realize that when using a directive with scope: true, transclude: true, Angular creates separate scopes for the directive and the transcluded content. This results in two scopes being generated, even if the group's scope is is ...

Troubleshooting tips for optimizing Opera and Internet Explorer performance

I'm currently on the hunt for solutions or techniques to debug my jquery script specifically under the Opera/IE browser. It appears that the ajax $.post() request is either not being sent at all, or it's being sent to the wrong address, among oth ...

I'm currently working on a Vue template that incorporates Vuetify, and I am interested in integrating Nuxt.js into the template

Hello there, I trust you are doing splendidly I am interested in learning how to incorporate Nuxt js into my Vue template to harness the advantages it offers such as file structure and simplified routing ..etc Do you happen to have any guidance on this ...

The combination of NextJS and Firebase Auth using the GoogleAuthProvider is powerful

I am encountering challenges while trying to integrate firebase authentication with Google Provider in NextJS. I have set up the necessary environment variables and successfully established a connection with firebase. However, I keep running into an error ...

The specified type 'Observable<string | null>' cannot be assigned to type 'string'

I am struggling with the following code: id$!: Observable<string | null>; characterDetail$!: Observable<CharacterData | undefined>; //?????? constructor( private router: ActivatedRoute, private store$: Store ) {} ngOnInit(): void ...

UI-grid: Triggering a modal window from the filter header template

Is there a way to create a filter that functions as a simple modal window triggered by a click event, but can be displayed on top of a grid when placed within the filterHeaderTemplate? I have encountered an issue where the modal window I created is being ...

Utilizing the "apply" method with JavaScript OOP callbacks for dynamic context management

Encountering an issue while creating a callback in my custom EACH function utilizing Object-Oriented Programming principles. To achieve this, I have developed a JavaScript library that emulates the familiar habits of JQUERY. Experience it in action by ch ...

Enabling and disabling links in a Bootstrap dropdown menu with dynamic functionality on click

One interesting feature of bootstrap is that it allows users to disable links within its dropdown menu component by simply adding a class="disabled" into the corresponding <li> tag. I am looking to create some Javascript code so that when a user sel ...

Something went wrong: [ERR_UNSUPPORTED_ESM_URL_SCHEME]: The default ESM loader only supports URLs with a scheme of file, data, and node

Every time I attempt to run yarn dev to start a discord bot, I encounter the following error. The bot is built using TypeScript. I have attempted to update the Node.js version using nvm, but to no avail. I even tried using older versions of Node.js, going ...

Mongoose opts for the __v field over a traditional date field

My current model setup is causing unexpected behavior: const mongoose = require("mongoose"); const Schema = mongoose.Schema; const NewModelSchema = new Schema({ user: { type: Schema.Types.ObjectId, ref: "users", }, date: ...

Getting the Request Body Content in Express Middleware

Currently, I am in the process of developing a small API logger to use as an Express middleware. This logger is designed to gather data from both the request and response objects, then store this information in a JSON file on disk for later reference. Her ...

Issue with splitting a JavaScript function in JQuery within the Wordpress platform can cause errors

My split function seems to be causing an error, as shown in this console error image. There is a hidden input field within the PHP file: <input class="file-id" name="_file-id" type="hidden" value="<?php echo esc_attr($file_ids); ?>" /> I hav ...

Tips for automatically expanding all nodes with children when the page loads in the Wix Angular tree control

Is there a way to automatically expand all nodes with children when the page loads in an Angular tree control? The tree control is full of useful features, but this specific functionality seems to be missing. It does have a property for expanded nodes. Do ...

Activate the 'li' element within the 'ul' whenever a 'ui-sref' link on the

I have been working on building a dashboard that consists of an abstract state called dashboard, with subdashboard as the immediate child state. https://i.sstatic.net/YTHky.png Previously, clicking on user details would take the user to the dashboard.tab ...

Give a radio button some class

<input id="radio1" type="radio" name="rgroup" value="1" > <label for="radio1"><span><span></span></span>1</label> <input id="radio2" type="radio" name="rgroup" value="2" > <label for="radio2"><span ...